数据库的连接及数据读取,显示操作

1。目前的ADO.net   目前 ADO.NET 提供了两种托管提供程序:一种用于 SQL Server 7.0 或更高版本,另一种用于其他所有您可能已经安装的 OLE DB 提供程序。在这两种情况下您分别使用不同的类,但遵循相似的命名规则。除前缀外,名称都是相同的。前一种情况前缀为 SQL,后一种情况则是 ADO。 <% @ Import Namespace="System.Data.ADO" %> <% @ Import Namespace="System.Data.SQL" %>   您应该使用 SQL 类访问 SQL Server 表,因为它们直接进入数据库服务器的内部 API,跳过了由 OLE DB 提供程序表示的中间层。ADO 类是 OLE DB 提供程序上的 .NET 接口,它们使用 COM Interop 桥进行工作。 2。连接一个数据库 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() 关闭 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,否则会很耗服务器资源的。 4。数据的显示   在这节的讲解前,我们先建立一个数据库,名字叫 aspnet 然后里面有一张表user 结构如下: uid username Email 1 User1 Mail1 2 User2 Mail2 3 User3 Mail3   SQL 语句 Select * From User   数据库语句 server=localhost;uid=sa;pwd=;database=aspnet 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["字段名"]的方法也可以取到数据 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。数据的添加,修改,和删除   其实他们是在就是简单的不要再简单的东西。   添加: 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") 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"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值