时间:2021-07-01 10:21:17 帮助过:59人阅读
'以下为建立connection对象连接数据库
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
'以下为判断是否连接
If conn.state=2 Then
Response.Write("正在连接")
ElseIf conn.state=1 Then
Response.Write("连接已经打开")
Else
Response.Write("对不起,无法打开连接")
End If
(2)、SQL SERVER用户名身份验证连接方法,此例中SQL用户名为sa ,密码********
代码如下:
'以下为建立connection对象连接数据库
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
'以下为判断是否连接
If conn.state=2 Then
Response.Write("正在连接")
ElseIf conn.state=1 Then
Response.Write("连接已经打开")
Else
Response.Write("对不起,无法打开连接")
End If
(3)、以上两种方法也可以用以下方式
代码如下:
Dim SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName,ConnStr,Conn
SqlDatabaseName = "forum" 'SQL数据库名
SqlUsername = "sa" 'SQL数据库用户名
SqlPassword = "**********" 'SQL数据库用户密码
SqlLocalName = "WWW-2443D34E558\SQL2005" 'SQL主机IP地址(服务器名)
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("正在连接")
ElseIf conn.state=1 Then
Response.Write("连接已经打开")
Else
Response.Write("对不起,无法打开连接")
End If