ASP. NET database connection method
What is ADO. NET?
ADO. NET is an integral part. NET Framework
ADO. NET is a group of classes used to process data access.
ADO. NET is completely XML-based
ADO. NET is also different. With ADO, there is no Recordset object
Create a database connection
We will use the example of the Northwind database.
First, import the "System. Data. OleDb" namespace. We need to name this with Microsoft Access and other ole db database vendors. We will create a connection to the database in the Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class connection string to identify the database of the ole db provider and location. Then, we open the 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
</Script>
Note: The connection string must be a continuous string with no broken lines! Create a database command to specify the records to be retrieved from the database. We will create a dbcomm variable as a new OleDbCommand class. This OleDbCommand class issues SQL query 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 MERs"
Dbcomm = New OleDbCommand (SQL, dbconn)
End sub
</Script> Create a DataReader. The OleDbDataReader class is used to read stream records from a data source. A DataReader is the OleDbCommand object for creating the call ExecuteReader method: <% @ 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 MERs"
Dbcomm = New OleDbCommand (SQL, dbconn)
Dbread = dbcomm. ExecuteReader ()
End sub
</Script>