Establishing a connection through driver
The code is as follows |
Copy Code |
<% Const databasetype=1 If Databasetype=0 Then Dbpath= "/jb51/news.asp" Sqlnowstring = "Now ()" Ystr=true Nstr=false Suiji= "Rnd (ID)" Else ' If it is a SQL database, please carefully modify the following database options DataServer = "www111cnnet" ' Database server IP Datauser = "Jb51net" ' Access to database user name DataBaseName = "Jb51net" database name DATABASEPSW = "Password" access to the database password sqlnowstring = "getdate ()" Ystr=1 Nstr=0 Suiji= "newid ()" End If On Error Resume Next If DatabaseType = 1 Then Connstr= "Driver={sql server};server=" "&dataserver&"; Uid= "&datauser&"; Pwd= "&databasepsw&";D atabase= "&databasename Else ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath (DBPath) End If Set conn = Server.CreateObject ("ADODB. Connection ") Conn.Open ConnStr If ERR Then Err.Clear:Set conn = Nothing:Response.Write database connection error, check the database parameter settings in the Conn.asp file. ": Response.End %> |
Establishing a connection through driver
It is also not necessary to create an ODBC DSN data source by driver the connection between the page and the database, but you must know the actual database file path or the data source name (for example, SQL Server database).
The code is as follows |
Copy Code |
Set conn=server.createobject ("ADODB. Connection ") Conn. Open "Driver={sql server};server=abc;datasource= (test); uid=;p wd=;d Atabase=userdb"
|
Writing scripts and database sources to establish a connection
The ADO (ActiveX Data Objects) provides a Connection object that you can use to establish and manage connections between applications and ODBC databases. The Connection object has a variety of properties and methods that you can use to open and close a database connection. To write a database connection script, you should first create an instance of the Connection object, and then open the database connection
code is as follows |
copy code |
' ********** connection with SQL Server2000 ' You can reference it to establish your database connection ' * * * ' Please note: ' Configure strserver,struid as appropriate, Strsapwd,strdbname four parameters Dim strserver,struid,strsapwd,strdbname strserver= "(local)" Database server name struid= TestUser "' Your login account strsapwd=" 12345 "' Your login password strdbname= ' db_test_com ' your database name Dim Conn ' Database Connection Dim strDSN ' Database connection string Dim Rs ' command string strdsn= ' driver={sql server};server= ' &StrServer& uid= ' &struid & ";p wd=" &StrSaPwd& ";d atabase=" &strdbname Connection to database Master Set Conn = Server.CreateObject (" ADODB. Connection ") Set Rs=server.createobject (" ADODB. RecordSet ") Conn.Open strdsn |