<% @ Import Namespace="System.Data.ADO" %> <% @ Import Namespace="System.Data.SQL" %> |
Dim myConnection As New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs") Dim myCommand As New SQLDataSetCommand("select * from Authors", myConnection) 或者 SQLConnection myConnection = new SQLConnection(); myConnection.DataSource = "localhost"; myConnection.UserID = "sa"; myConnection.Password = ""; myConnection.ConnectionTimeout = 30; myConnection.Open(); myConnection.Database = "pub"; myConnection.IsolationLevel = IsolationLevel.ReadCommitted |
这里我们需要讲述一下Connection的方法和属性了。
ConnectionTimeout超时
DataBase 缺省数据库
DataSource DNS
UserID 原来叫UID
Password
State 取得目前连接的状态
Open() 打开
Close() 关闭
5.3 操作数据库
通过一个Command对象,我们才可以对数据库进行操作
Dim myConnection As SQLConnection = New SQLConnection("server=localhost;uid=sa; pwd=;database=pubs") Dim myCommand As SQLCommand = New SQLCommand("select * from Authors", myConnection) myConnection.Open() Dim dr As New SQLDataReader myCommand.Execute(dr) ... myConnection.Close() 或者 这样做 Dim myConnection As New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs") Dim mycommand As New SQLCommand( _ "UPDATE Authors SET phone='(800) 555-5555' WHERE au_id = '123-45-6789'", _ myConnection) myCommand.ActiveConnection.Open() myCommand.ExecuteNonQuery() myCommand.ActiveConnection.Close() |
这些都是SQLCommand的标准用法,下面列出了Command的所有属性和相关方法。
ActiveConnection 取得或设置联结Connections
CommandText 执行的SQL语句或储存过程(StoredProcedure)名
CommandTimeout 超时
CommandType Command操作的类型(StoredProcedure,Text,TableDirect)三种,默认Text
Parameters 操作储存过程时使用
Execute() 执行SQL语句或储存过程
ExecuteNonQuery() 同上,但无返回,或者说,只返回记录的数量
注意: 和ASP一样,在运行完以后一定要注意关闭Connection,否则会很耗服务器资源的。
5.4 数据的显示
在这节的讲解前,我们先建立一个数据库,名字叫 aspnet 然后里面有一张表user 结构如下:
uid | username | |
1 | User1 | Mail1 |
2 | User2 | Mail2 |
3 | User3 | Mail3 |
SQL 语句 Select * From User
数据库语句 server=localhost;uid=sa;pwd=;database=aspnet
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> |
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") |
<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"/> |