The
The SqlDataSource control uses ADO.NET classes to interact with any database supported by ADO.NET. This includes Microsoft SQL Server (using the
Connecting the SqlDataSource Control to a Data Source
When you configure a SqlDataSource control, you set the
Instead of setting connection strings at design time as property settings in the SqlDataSource control, you can store them centrally as part of your application's configuration settings using the MyNorthwind
.
Visual BasicВ | ![]() |
---|---|
<%@ Page language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </FORM> </BODY> </HTML> |
C#В | ![]() |
---|---|
<%@ Page language="CS" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </FORM> </BODY> </HTML> |
J#В | ![]() |
---|---|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" SelectCommand="SELECT LastName FROM Employees"> </asp:SqlDataSource> <asp:ListBox id="ListBox1" runat="server" DataTextField="LastName" DataSourceID="SqlDataSource1"> </asp:ListBox> </FORM> </BODY> </HTML> |
Issuing Data Commands with the SqlDataSource Control
You can specify up to four commands (SQL queries) for the SqlDataSource command: a
You can create parameterized commands that include placeholders for values to be supplied at run time. The following example shows a typical parameterized SQL Select command:
Select CustomerID, CompanyName From Customers Where City =
@city
You can create parameter objects that specify where the command should get parameter values at run time, such as from another control, from a query string, and so on. Alternatively, you can specify parameter values programmatically. For more information, see Using Parameters with the SqlDataSource Control.
The data source control executes the commands when its corresponding
Returning DataSet or DataReader Objects
The SqlDataSource control can return data in two forms: as a
Caching with the SqlDataSource Control
The SqlDataSource control can cache data that it has retrieved, which can enhance the performance of your applications by avoiding expensive queries. Caching is practical in almost any situation where the data is not highly volatile and the cached results are small enough to avoid utilizing too much system memory.
Caching is not enabled by default. You can enable it by setting
The SqlDataSource control can also take advantage of the cache dependency feature of SQL Server (if available in your version of SQL Server). This feature allows you to specify that the data in the cache is maintained until SQL Server reports a change in the specified table. This type of caching allows you to improve the performance of data access in your Web applications, because you can minimize data retrieval to only those times when it is necessary to get refreshed data.
For more information, see Caching Data with the SqlDataSource Control.
Filtering with the SqlDataSource Control
If you have enabled caching for the SqlDataSource control and have specified a dataset as the format for data returned by a Select query, you can also filter the data without re-running the query. The SqlDataSource control supports a
Sorting with the SqlDataSource Control
The SqlDataSource control supports sort requests from the bound control when the DataSourceMode is set to