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,

<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server"><title>ASP.NET SQLDataSource Example</title></head>
 <body><form id = "form1" runat = "server">
  <asp:SqlDataSource id = "SqlDataSource1"
    runat = "server" DataSourceMode = "DataReader"
    ConnectionString = "<%$ ConnectionStrings:MyNorthwind%>"
    SelectCommand    = "SELECT FirstName, LastName, Title FROM Employees">
  </asp:SqlDataSource>
  <asp:GridView id = "GridView1" DataSourceID = "SqlDataSource1" runat="server" />
 </form></body>
</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,

<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server"><title>ASP.NET AccessDataSource Example</title></head>
 <body><form id = "form1" runat = "server">
  <asp:accessdatasource id = "AccessDataSource1" runat = "server"
    datasourcemode = "DataSet" datafile = "~/App_Data/Northwind.mdb"
    selectcommand = "SELECT EmployeeID, FirstName, LastName, Title FROM Employees"
    updatecommand = "UPDATE Employees SET FirstName=?, LastName=?, Title=?
      WHERE EmployeeID = @EmployeeID" >
  </asp:accessdatasource>
  <asp:gridview id = "GridView1" runat = "server"
    autogeneratecolumns   = "False" datakeynames = "EmployeeID"
    autogenerateeditbutton = "True" datasourceid = "AccessDataSource1">
   <columns>
    <asp:boundfield headertext = "First Name" datafield = "FirstName" />
    <asp:boundfield headertext = "Last Name"  datafield = "LastName" />
    <asp:boundfield headertext = "Title"      datafield = "Title" />
   </columns>
  </asp:gridview>
 </form></body>
</html>