This article describes the use of ASP to connect SQL Server database instance code, the connection method has three kinds of connections through ODBC DSN, through OLE DB to establish a connection through the driver to establish a connection of three kinds, the following we look at the first.
Establishing a connection through driver
code is as follows |
&nbs P; |
<%&NBSP; Const databasetype=1 If databasetype=0 then& nbsp dbpath= "/jb51/news.asp" sqlnowstring = "Now ()" &NBSP; ystr=true nstr=false Suiji= "Rnd (ID)" else ' If it is an SQL database, carefully modify the following database options DataServer = ' www111cnnet ' database server IP Datauser = "jb51net" Access database username DataBaseName = "jb51net" ' Database name databasepsw = ' Password ' Access database Password & nbsp Sqlnowstring = "getdate ()" ystr=1 nstr=0 suiji= "newid ()" &NBSP; end if On Error Resume next If databasetype = 1 then connstr= "Driver={sql server};server=" &dataserver& amp; "; 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" Database connection error. Please 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 |
|
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 |
&nbs P; |
' **************************************************************** connection with SQL Server2000 ' can reference it to establish your database connection ' ********************************************************** Please note: ' Configure strserver,struid,strsapwd,strdbname four parameters Dim strserver,struid,strsapwd as appropriate. 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 serv Er};server= "&StrServer&" uid= "&StrUid&";p wd= "&StrSaPwd&";d atabase= "&strdbname " Establish a connection to the database master Set Conn = Server.CreateObject ("ADODB. Connection ") Set Rs=server.createobject (" ADODB. RecordSet ") Conn.Open strdsn |