Objective: To connect to two databases
For ASP, the common databases are access and SQL Server, and ASP. NET. However, ASP. NET has dedicated connection components for SQL Server, and OLE DB is not recommended.
First, let's take a look at the ACCESS database connection and open it;
String strconnection = "provider = Microsoft. Jet. oledb.4.0; Data Source = ";
Strconnection + = server. mappath ("*. mdb"); // * indicates the database name.
Oledbconnection objconnection = new oledbconnection (strconnection );
Objconnection. open ();
Dim objconnection as oledbconnection
Objconnection = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("*. mdb "))
Objconnection. open ()
Next, let's take a look at how SQL server connects to and opens the database;
String strconnection = "Server = database connection; uid = user name; Pwd = password; database = database name ";
Sqlconnediob objconnection = new sqlconnection (strconnection );
Objconnection. open ();
Dim objconnection as sqlconnectiom
Objconnection = new sqlconnection ("Server = database connection; uid = username; Pwd = password; database = database name ")
Objconnection. open ()
In fact, in most cases, the difference between SQL Server and access is not only the connection statement, but also the difference between other definition statements, namely SQL ×× and oledb ××.
In addition, if it is an Access database, the following statements must be included at the beginning of the aspx file:
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. oledb" %>
For SQL Server, the following statements are required:
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. sqlclient" %>
Let's talk about database reading tomorrow.
Author's blog:Http://blog.csdn.net/byebye8742/