dim ac_tmp as new adodb.connection With ac_tmp If .State = adStateOpen Then .Close .ConnectionString = "driver=SQL Server;server=" & 计算机名或IP地址 _ & ";uid=sa;pwd=;database=" & 数据库名 .CommandTimeout = 50 .Open End With
数据库连接字符串的写法 第一种为你需要的Public Sub ConnectionStringX() Dim cnn1 As ADODB.Connection Dim cnn2 As ADODB.Connection Dim cnn3 As ADODB.Connection Dim cnn4 As ADODB.Connection ' Open a connection without using a Data Source Name (DSN). Set cnn1 = New ADODB.Connection cnn1.ConnectionString = "driver={SQL Server};" & _ "server=数据库IP地址;uid=sa;pwd=pwd;database=pubs" cnn1.ConnectionTimeout = 30 cnn1.Open
' Open a connection using a DSN and ODBC tags. Set cnn2 = New ADODB.Connection cnn2.ConnectionString = "DSN=Pubs;UID=sa;PWD=pwd;" cnn2.Open
' Open a connection using a DSN and OLE DB tags. Set cnn3 = New ADODB.Connection cnn3.ConnectionString = "Data Source=Pubs;User ID=sa;Password=pwd;" cnn3.Open
' Open a connection using a DSN and individual ' arguments instead of a connection string. Set cnn4 = New ADODB.Connection cnn4.Open "Pubs", "sa", "pwd"
' Display the state of the connections. MsgBox "cnn1 state: " & GetState(cnn1.State) & vbCr & _ "cnn2 state: " & GetState(cnn2.State) & vbCr & _ "cnn3 state: " & GetState(cnn3.State) & vbCr & _ "cnn4 state: " & GetState(cnn4.State) cnn4.Close cnn3.Close cnn2.Close cnn1.CloseEnd Sub
'连接数据库 Public Function OpenDB() As Boolean On Error GoTo Err ErrMsg = "" Set Conn = CreateObject("adodb.connection") Conn.CursorLocation = 3 'ConnStr = "driver={SQL Server};server=zh;uid=sa;pwd=;database=test" Conn.Open ConnStr OpenDB = True Exit Function Err: Set Conn = Nothing ErrMsg = Err.Number & ":" & Err.Description OpenDB = False End Function
Connect via an IP address: "Provider=sqloledb;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;" (DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))
With ac_tmp
If .State = adStateOpen Then .Close
.ConnectionString = "driver=SQL Server;server=" & 计算机名或IP地址 _
& ";uid=sa;pwd=;database=" & 数据库名
.CommandTimeout = 50
.Open
End With
第一种为你需要的Public Sub ConnectionStringX() Dim cnn1 As ADODB.Connection
Dim cnn2 As ADODB.Connection
Dim cnn3 As ADODB.Connection
Dim cnn4 As ADODB.Connection ' Open a connection without using a Data Source Name (DSN).
Set cnn1 = New ADODB.Connection
cnn1.ConnectionString = "driver={SQL Server};" & _
"server=数据库IP地址;uid=sa;pwd=pwd;database=pubs"
cnn1.ConnectionTimeout = 30
cnn1.Open
' Open a connection using a DSN and ODBC tags.
Set cnn2 = New ADODB.Connection
cnn2.ConnectionString = "DSN=Pubs;UID=sa;PWD=pwd;"
cnn2.Open
' Open a connection using a DSN and OLE DB tags.
Set cnn3 = New ADODB.Connection
cnn3.ConnectionString = "Data Source=Pubs;User ID=sa;Password=pwd;"
cnn3.Open
' Open a connection using a DSN and individual
' arguments instead of a connection string.
Set cnn4 = New ADODB.Connection
cnn4.Open "Pubs", "sa", "pwd"
' Display the state of the connections.
MsgBox "cnn1 state: " & GetState(cnn1.State) & vbCr & _
"cnn2 state: " & GetState(cnn2.State) & vbCr & _
"cnn3 state: " & GetState(cnn3.State) & vbCr & _
"cnn4 state: " & GetState(cnn4.State) cnn4.Close
cnn3.Close
cnn2.Close
cnn1.CloseEnd Sub
Public Function OpenDB() As Boolean On Error GoTo Err
ErrMsg = ""
Set Conn = CreateObject("adodb.connection")
Conn.CursorLocation = 3
'ConnStr = "driver={SQL Server};server=zh;uid=sa;pwd=;database=test"
Conn.Open ConnStr
OpenDB = True
Exit Function
Err:
Set Conn = Nothing
ErrMsg = Err.Number & ":" & Err.Description
OpenDB = False
End Function
"Provider=sqloledb;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))
mCnn.CommandTimeout = 60 * 30
mCnn.Provider = "MSDataShape"
mCnn.Open "Data Provider=SQLOLEDB.1;" & _
"Persist Security Info=False;" & _
"User ID=" + mDBUserID + _
";Password=" + mDBUserPWD + _
";Initial Catalog=" + vDBName + ";" & _
"Data Source=" & mDBServerName
http://expert.csdn.net/Expert/topic/2710/2710006.xml?temp=8.526248E-02