Slide 7.13: Embedding SQL in ASP.NET
Slide 7.15: Embedding SQL in ASP.NET (cont.)
Home

Embedding SQL in ASP.NET (Cont.)


SqlDataSource (shown in the next slides)
It is a data source control that represents a connection to an ADO.NET SQL database provider, such as SQL, OLEDB, ODBC, or Oracle (e.g., SqlDataSource using the Microsoft Jet 4.0 OLEDB provider to connect to Access). For example,

01<%@ Page Language="C#" %>
03 <head runat="server"><title>ASP.NET SQLDataSource Example</title></head>
04 <body><form id = "form1" runat = "server">
05  <asp:SqlDataSource id = "SqlDataSource1"
06    runat = "server" DataSourceMode = "DataReader"
07    ConnectionString = "<%$ ConnectionStrings:MyNorthwind%>"
08    SelectCommand    = "SELECT FirstName, LastName, Title FROM Employees">
09  </asp:SqlDataSource>
10  <asp:GridView id = "GridView1" DataSourceID = "SqlDataSource1" runat="server" />
11 </form></body>
12</html>

AccessDataSource (a newer version of SqlDataSource)
It is a data source control that represents a connection to an Access database. The AccessDataSource control derives from SqlDataSource, but exposes a simple DataFile property for specifying the path to an MDB file, instead of the complete connection string. For example,

01<%@ Page Language="C#" %>
03 <head runat="server"><title>ASP.NET AccessDataSource Example</title></head>
04 <body><form id = "form1" runat = "server">
05  <asp:accessdatasource id = "AccessDataSource1" runat = "server"
06    datasourcemode = "DataSet" datafile = "~/App_Data/Northwind.mdb"
07    selectcommand = "SELECT EmployeeID, FirstName, LastName, Title FROM Employees"
08    updatecommand = "UPDATE Employees SET FirstName=?, LastName=?, Title=?
09      WHERE EmployeeID = @EmployeeID" >
10  </asp:accessdatasource>
11  <asp:gridview id = "GridView1" runat = "server"
12    autogeneratecolumns   = "False" datakeynames = "EmployeeID"
13    autogenerateeditbutton = "True" datasourceid = "AccessDataSource1">
14   <columns>
15    <asp:boundfield headertext = "First Name" datafield = "FirstName" />
16    <asp:boundfield headertext = "Last Name"  datafield = "LastName" />
17    <asp:boundfield headertext = "Title"      datafield = "Title" />
18   </columns>
19  </asp:gridview>
20 </form></body>
21</html>