Dynamic web development ASP connection SQL2005 database connection code description:
a. In the following characters, the database name is forum and the database server name is WWW-2443D34E558/SQL2005 (or 127.0.0.1)
b. Check the sql database server name: Object Explorer->Database->Right-click on the database you created->Properties->Permissions
c. Provider=sqlncli can also be used. There cannot be a space between UID and =. If you use user id instead, there can be
Space, Initial Catalog can be replaced with database, pwd can be replaced with password, and data source can be replaced with server.
In VBScript, there is no case sensitivity, so UID is equivalent to uid is equivalent to Uid. . . , where provider, uid, pwd,
Initial Catalog, data source and other locations can be placed freely.
(1) Windows authentication connection SQL2005 string, use Integrated Security=SSPI method
Copy the code code as follows:
'The following is to establish a connection object to connect to the database
Dim conn
Set conn=Server.CreateObject(ADODB.Connection)
sql=Provider=SQLoledb;data source=WWW-2443D34E558/SQL2005;UID=;PWD=;Initial Catalog=forum;Integrated Security=SSPI
conn.Open sql
'The following is to determine whether to connect
If conn.state=2 Then
Response.Write(Connecting)
ElseIf conn.state=1 Then
Response.Write (the connection is already open)
Else
Response.Write(Sorry, the connection cannot be opened)
End If
(2) SQL SERVER user name authentication connection method. In this example, the SQL user name is sa and the password is ********
Copy the code code as follows:
'The following is to establish a connection object to connect to the database
Dim conn
Set conn=Server.CreateObject(ADODB.Connection)
sql=Provider=SQLoledb;data source=WWW-2443D34E558/SQL2005;UID=sa;PWD=********;Initial Catalog=forum
conn.Open sql
'The following is to determine whether to connect
If conn.state=2 Then
Response.Write(Connecting)
ElseIf conn.state=1 Then
Response.Write (the connection is already open)
Else
Response.Write(Sorry, the connection cannot be opened)
End If
(3) The above two methods can also be used in the following ways
Copy the code code as follows:
Dim SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName,ConnStr,Conn
SqlDatabaseName = forum 'SQL database name
SqlUsername = sa 'SQL database username
SqlPassword = ********** 'SQL database user password
SqlLocalName = WWW-2443D34E558/SQL2005 'SQL host IP address (server name)
ConnStr = Password = &SqlPassword&; user id = & SqlUsername&; Initial Catalog =&SqlDatabaseName&; data source =& SqlLocalName &;Provider = sqloledb;
Set conn = Server.CreateObject(ADODB.Connection)
conn.open ConnStr
If conn.state=2 Then
Response.Write(Connecting)
ElseIf conn.state=1 Then
Response.Write (the connection is already open)
Else
Response.Write(Sorry, the connection cannot be opened)
End If