艺术迷网
艺术迷 >> 网页设计 >> Asp.net >> ASP.NET教程第五讲 数据库的操作
2006-11-13   作者:wangjh   来源:中国dotnet论坛   点击:1578
页面功能: 【评论】有错就点】 【字体: 】 【打印】 【关闭【收藏本页】收藏到365Key
 


5.4.1 用 DataReader 方法显示数据

  有两种方法可以显示数据 DataReader方法,和DataSet方法,而DataReader只能储存查询数据,我们先讲用DataReader方法显示



<script language="VB" runat="server">
 Sub Page_Load(Src As Object, E As EventArgs)
  Dim MyConnection As SQLConnection =
     New SQLConnection("server=localhost;uid=sa;
               pwd=;database=aspnet")
  Dim MyCommand As SQLCommand =
     New SQLCommand("select * from User", MyConnection)
  MyConnection.Open()
  Dim DR As SQLDataReader
  MyCommand.Execute(DR)
  MyDataGrid.DataSource = DR
  MyDataGrid.DataBind()
  MyConnection.Close()
 End Sub
</script>
<ASP:DataGrid id="MyDataGrid" runat="server"
   Width="700"
   BackColor="#ccccff"
   BorderColor="black"
   ShowFooter="false"
   CellPadding=3
   CellSpacing="0"
   Font-Name="Verdana"
   Font-Size="8pt"
   HeaderStyle-BackColor="#aaaadd"
/>


  对于显示的控制,大家可以复习一下前一讲,数据的绑定,其实多联系也是一种很好的方法。

  在定制显示中,还有一种比较使用的方法,而不用绑定



Dim DR As SQLDataReader


  DR["字段名"]的方法也可以取到数据

  5.4.2 用 DataSet 方法显示数据

  用DataSet记录的数据其实就是一个表,而对表的操作,只是对DataSet的操作,并没有改变数据库,而要到DataSet更新的时候,才完整的写入数据库,这个往往是新手容易忽视的地方。



<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

<html>
<script language="VB" runat="server">

 Sub Page_Load(Src As Object, E As EventArgs)

  Dim DS As DataSet
  Dim MyConnection As SQLConnection
  Dim MyCommand As SQLDataSetCommand
  
  MyConnection = New
   SQLConnection("server=localhost;uid=sa;pwd=;database=aspnet")
  MyCommand =
   New SQLDataSetCommand("select * from User",MyConnection)

  DS = new DataSet()
   ~~~初始化DataSet()
  MyCommand.FillDataSet(ds, "User")
   ~~~FillDataSet顾名思义把整个查询内容储存进DataSet中
  MyDataGrid.DataSource=ds.Tables("User").DefaultView
  MyDataGrid.DataBind()
   ~~~绑定数据
 End Sub
</script>
<body>

 <h3><font face="Verdana">
  Simple Select to a DataGrid Control
</font></h3>

<ASP:DataGrid id="MyDataGrid" runat="server"
  Width="700"
  BackColor="#ccccff"
  BorderColor="black"
  ShowFooter="false"
  CellPadding=3
  CellSpacing="0"
  Font-Name="Verdana"
  Font-Size="8pt"
  HeaderStyle-BackColor="#aaaadd"
  MaintainState="false"
/>

</body>
</html>


5.5 数据的添加,修改,和删除

  其实他们是在就是简单的不要再简单的东西。

  添加:

DataRow dr=SQLDataSet.Tables["User"].NewRow();
dr["id"] = "4";
dr["username"] = "user4";
dr["Email"] = "mail4";
SQLDataSet.Tables.Rows.Add(dr);
修改:
SQLDataSet.Tables["user"].Rows[3]["username"]= "user5"
删除:
SQLDataSet.Tables["user"].Rows[3].Delete()
修改完之后,必须更新数据库
SQLCommand.Update(SQLDataSet, "user")

  5.6 关于显示中的分页问题

  这个问题,一再在论坛中给众人提出过,曾经是ASP中,一个比较难解决的问题,不过在ASP.net中,只不过是DataGrid的一个属性而已。

  AllowPaging="True" 是否支持分页

  PageSize="10" 每页显示多少

  PagerStyle-HorizontalAlign="Left" 分页显示的定位

  完整的例子:

<asp:DataGrid id="dataGrid1" runat="server"
  BorderColor="black"
  BorderWidth="1"
  GridLines="Both"
  CellPadding="3"
  CellSpacing="0"
  HeaderStyle-BackColor="#aaaadd"
  AllowPaging="True"
  PageSize="10"
  PagerStyle-HorizontalAlign="Left"/>


ASP.NET教程第五讲 数据库的操作

1|2|
声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。如有疑问请与站长联系。
网友评论
目前没有评论!
发表评论
您的姓名: 匿名发送
电子邮件:
评论内容:
不能超过100个字符



所有留言只代表网友个人观点,不代表本站观点。
请各位遵纪守法并注意语言文明。
新闻搜索
关 键 词:
搜索范围:
全站精华
图文精彩