The SqlDataReader class
This class is used to create a data reader object, which provides an efficient way to read the rows in a result set returned by a database query. In fact, when you use a data adapter to retrieve data, the data adapter uses a data reader to read through the rows in the result set and store them in a dataset.

  • A SqlConnection object is required to establish a connection to a SQL Server database.
  • A SqlCommand object is used to execute a SQL command against a SQL Server database.
  • A SqlParameter object is used to pass variable information to a SQL command.

The SqlParameter class lets you pass parameter values to a SQL command. Parameters arc commonly used to limit the number of rows retrieved by a Select statement. For example, you can retrieve the Product row for a specific product by passing the ProductlD as a parameter. Or, you can retrieve all of the products for a given category by passing the CategorylD as a parameter. Parameters are also commonly used to pass column values to Insert, Update, and Delete statements.

Aspx code generated for a basic SqlDataSource control
<asp:SqlDataSource ID="SqlDataSourcel" runat="server" ConnectionString="<%$ConnectionStrings:w3aspConnectionString%>"
SelectCommand="SELECT [CategoryID], [LongName] FROM [Categories]
ORDER BY [LongName]">

The web.config file has a connectionStrings element that contains an add element for each connection string. In the example, the connection string is named w3aspConnectionString, and the connection string refers to a database named w3asp on the server named localhost\sqlexpress.

PreviousDisplaying 4 of 4
1 2 3 4
Need Help? Contact Us.

Log in


Forgot password?


New User