数据库的连接及数据读取,显示操作
2005-04-08 23:32
603 查看
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"/>
目前 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"/>
相关文章推荐
- 访问网站时只是显示主页(index.jsp),没有请求连接数据库读取数据。
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- Java基于JDBC连接数据库及显示数据操作示例
- 訪问站点时仅仅是显示主页(index.jsp),没有请求连接数据库读取数据。
- dataset 操作数据库 读取数据并列表显示
- 如何读取配置文件并连接后台数据库,且对数据库进行操作实例(查询或修改等)
- python web py入门(4)-从数据库读取数据显示到网页
- java连接数据库和读取数据
- (四)、读取数据库数据并在HighCharts上显示
- 连接数据库(保存和读取数据)
- java连接数据库并显示数据
- QLite数据库的数据读取和写入 通过界面操作数据库
- uDig连接PostgreSQL显示shp数据操作步骤
- [转]数据库连接方式读取不到Excel数据值的解决方法
- VC中连接mdb数据库及其数据读取方法
- springmvc从数据库中读取数据并且显示成选择框工具类
- 3.如何优化操作大数据量数据库(实现小数据量和海量数据的分页显示存储过程)
- Echarts-Struts2+Spring+Mybnatis+Jquery+Json在页面显示数据(数据从数据库读取)
- Thinkphp 连接数据库操作以及如何创建数据
- hibernate 连接数据库操作步骤以及对数据的增删查改