Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get data from a SQL View using ADO.Net

Author  Topic 

Webskater
Starting Member

19 Posts

Posted - 2008-02-19 : 12:04:27
When using ADO.Net to retrieve data from SQL Server into a SQLDataReader I use this code in a Data Access class:


public SqlDataReader GetView(string ViewName)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = ViewName;
myCommand.CommandTimeout = 120;

// Mark the Command as a SPROC
[COLOR="Red"]myCommand.CommandType = CommandType.StoredProcedure[/COLOR];

// Execute the stored procedure and Return the datareader result
myConnection.Open();
return myCommand.ExecuteReader(CommandBehavior.CloseConnection)


This works fine when I am getting the data from a Stored Procedure.

But, on this occasion, I want to get the data from a View. How can I do this? There does not seem to be an option of CommandType.View

Thanks for any help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-19 : 12:12:27
Use CommandType.text and use view as if you use a table (a view is nothing but a virtual table)
Go to Top of Page

Webskater
Starting Member

19 Posts

Posted - 2008-02-20 : 06:06:55
Thanks for your answer.
Go to Top of Page

Webskater
Starting Member

19 Posts

Posted - 2008-02-20 : 06:08:06
Is there a way of marking threads as 'answered' on this forum?
Go to Top of Page
   

- Advertisement -