第四步:在主页面生成了一个SqlDataSource数据源控件的同时,还显示如下的窗口:


  点击"新建连接"按钮,显示的窗体如下:



  "服务器名"为你本地的SQL Server上的一个实例,填写好了,你就可以选择2种登录到服务器的方式:


  Windows验证和SQL Server验证两种,它们的区别我将在后面进行详细的讲解。我们在这里先选择Windows验证的方式,在选择"pubs"数据库后点击"测试连接"如果显示连接成功后就表明我们已经连接到SQL Server服务器上了。


  第五步:单击"确定"按钮后,将显示"配置数据源"的窗体,我们选择"hoowoo.pubs.dbo"作为我们将要连接的数据库。


  第六步:由于我们希望将连接字符串保存到应用程序配置文件中,所以我们希望在web.config中保持这个连接字符串。在这里我们希望将连接字符串保存为一个别名:Pubs



  第七步:选择下图的"authors"表来选择我们所需要的数据,这里我们选择了所有的字段。


完成上面的所以步骤后,我们在主页面上的右键菜单上选择"查看代码",可以看到如下的代码:


<%@ Page Language="C#" %>
            <html>
             <head id="Head1" runat="server">
              <title>GridView Bound Fields</title>
             </head>
             <body>
              <form id="form1" runat="server">
               <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"AutoGenerateColumns="False" runat="server">
               <Columns>
                <asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />
                <asp:BoundField HeaderText="Last Name" DataField="au_lname" />
                <asp:BoundField HeaderText="First Name" DataField="au_fname" />
                <asp:BoundField HeaderText="Phone" DataField="phone" />
                <asp:BoundField HeaderText="Address" DataField="address" />
                <asp:BoundField HeaderText="City" DataField="city" />
                <asp:BoundField HeaderText="State" DataField="state" />
                <asp:BoundField HeaderText="Zip Code" DataField="zip" />
                <asp:CheckBoxField HeaderText="Contract" DataField="contract" />
               </Columns>
               </asp:GridView>
               <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone],[address],[city], [state], [zip], [contract] FROM [authors]" ConnectionString="<%$ ConnectionStrings:Pubs %>" />
              </form>
             </body>
            </html>


  Web.Config中的代码如下:


<?xml version="1.0"?>
            <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
            <appSettings/>
            <connectionStrings>
            <add name="Pubs" connectionString="Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
            </connectionStrings>
            <system.web>
            <compilation debug="false"/>
            <authentication mode="Windows"/>
            </system.web>
            </configuration>


  现在来重点分析这些代码的意义:


"<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"
            AutoGenerateColumns="False" runat="server">"


  数据绑定控件通过其 DataSourceID 属性连接到数据源控件,从而我们可以进行排序、分页、筛选、更新、删除和插入等一系列的操作。


"<Columns>
            <asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />
            <asp:BoundField HeaderText="Last Name" DataField="au_lname" />
            <asp:BoundField HeaderText="First Name" DataField="au_fname" />
            <asp:BoundField HeaderText="Phone" DataField="phone" />
            <asp:BoundField HeaderText="Address" DataField="address" />
            <asp:BoundField HeaderText="City" DataField="city" />
            <asp:BoundField HeaderText="State" DataField="state" />
            <asp:BoundField HeaderText="Zip Code" DataField="zip" />
            <asp:CheckBoxField HeaderText="Contract" DataField="contract" />
            </Columns>"


  "BoundField"和"CheckBoxField"均为要绑定的控件类型,"HeaderText"是将要显示在表格上字段的名称,而"DataField"则是我们要进行绑定的数据字段。


<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            SelectCommand="SELECT [au_id], [au_lname], [au_fname],
            [phone],[address],[city], [state], [zip], [contract] FROM [authors]"
            ConnectionString="<%$ ConnectionStrings:Pubs %>" />


  SqlDataSource控件中我们设置了数据库的SelectCommand命令为"SELECT [au_id],[au_lname],[au_fname],[phone],[address] [city], [state], [zip], [contract] FROM [authors]"这正好和GridView所要绑定的控件一一对应,这充分说明了数据绑定控件和数据源控件的紧密联系。


  细心的读者可能会奇怪了,ConnectionString="<%$ ConnectionStrings:Pubs %>在SqlDataSource是表示什么呢?这个问题就和我们为什么需要Web.Config配置文件有很大的关联了。Web.Config中设置了如下的节点:


<connectionStrings>
            <add name="Pubs" connectionString="Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
            </connectionStrings>


  我们可以通过检索Web.Config配置文件来取得数据库连接字符串别名"Pubs"的真正的含义是

"Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" Initial Catalog表明我们使用的是"pubs"数据库。Integrated Security说明了我们采用的是Windows验证方式。