ASP.NET - Rarruwar yauyau
- 上一页 DataList WebForms
- 下一页 WebForms 母版页
ADO.NET naɗa ɗanɗin ɗanɗin .NET.
Yanar ADO.NET ce yin aiki da rarruwar yauyau. Ta ADO.NET, ka iya aiki da yauyau ne.
Koye ADO.NET shi?
- ADO.NET naɗa ɗanɗin ɗanɗin .NET
- ADO.NET consists of a series of classes used to operate data access
- ADO.NET is completely based on XML
- ADO.NET does not have a Recordset object, unlike ADO
create database connection
We intend to use the Northwind database that we have used before.
First, import the namespace "System.Data.OleDb". We need this namespace to be able to operate Microsoft Access and other OLE DB data providers. We will create a connection to this database in the Page_Load subroutine. We created a dbconn variable and assigned a new OleDbConnection class to it, which has a connection string that can indicate OLE DB and the location of the database. Then we opened this database connection:
<%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> sub Page_Load dim dbconn dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("northwind.mdb")) dbconn.Open() end sub
Note:This connection string must be a continuous string without line breaks!
create database command
To specify the records to be retrieved from the database, we will create a dbcomm variable and assign a new OleDbCommand to it. This OleDbCommand class is used to issue SQL queries against database tables:
<%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> sub Page_Load dim dbconn,sql,dbcomm dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("northwind.mdb")) dbconn.Open() sql="SELECT * FROM customers" dbcomm=New OleDbCommand(sql,dbconn) end sub
create DataReader
The OleDbDataReader class is used to read a record stream from the data source. By calling the ExecuteReader method of the OleDbCommand object, you can create a DataReader:
<%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> sub Page_Load dim dbconn,sql,dbcomm,dbread dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("northwind.mdb")) dbconn.Open() sql="SELECT * FROM customers" dbcomm=New OleDbCommand(sql,dbconn) dbread=dbcomm.ExecuteReader() end sub
bind to Repeater control
then, we bind this DataReader to a Repeater control:
<%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> sub Page_Load dim dbconn,sql,dbcomm,dbread dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("northwind.mdb")) dbconn.Open() sql="SELECT * FROM customers" dbcomm=New OleDbCommand(sql,dbconn) dbread=dbcomm.ExecuteReader() customers.DataSource=dbread customers.DataBind() dbread.Close() dbconn.Close() end sub
关闭数据库连接
在不在需要访问数据库后,请记得始终将 DataReader 和数据库连接关闭:
dbread.Close() dbconn.Close()
- 上一页 DataList WebForms
- 下一页 WebForms 母版页