用户名: 密码: 免费注册 忘记密码? 网站地图 | 加入收藏 | 设为首页
首页 | 新闻 | 工具 | 系统 | 办公 | 聊天 | 多媒体 | 网页 | 运营 | 平面 | 欣赏 | 数据库 | 程序 | 服务器 | 组网
网页 | 3dmax | Ghost | Windows Xp| Dreamweaver | photoshop | Flash | office | Alexa | Css | QQ | Asp | PHP | Jsp | Access
Flash MX 2004入门 | 网站推广策略 | CorelDRAW入门 | ASP学习 | 网站建设大师功 | Word入门
  iTbulo.com > 学院 > 程序开发教程 > ASP.net教程 > Asp.Net基础教程 > 文章正文
ASP.NET 2.0轻松实现数据库应用开发
iTbulo.COM 2005-10-24 未知()

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

<%@ 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验证方式。

  最后的显示如下:

上一页  [1] [2] [3] [4] 

文章搜索
相关资讯
相关文章 相关下载
在ASP.NET中上传图片并生成缩略图
Excel在.Net下驻留内存的解决方法
ASP.NET中常用的优化性能方法详解
网友原创:从N层到.NET详细剖析原理
经验之谈:MySQL与ASP.NET配合更强大
焦点信息