Public Function ConnnectToDatabase() As Boolean
On Error GoTo Errhandler
Set g_conn = New ADODB.Connection
Dim ServerName As String, DBName As String, UserName As String, strPassword As String
ServerName = "(local)"
DBName = "meeting"
UserName = "sa"
strPassword = "sa"
With g_conn
.CursorLocation = adUseClient
.CommandTimeout = 100
.ConnectionString = "Provider=SQLOLEDB;Persist Security Info=true" & _
";User ID=" & UserName & ";Initial Catalog=" & DBName & _
"; Data Source=" & ServerName & ";Password=" & strPassword
.Open
End With
ConnectToDatabase = True
Exit Function
Errhandler
ConnectionToDatabase = False
MsgBox Err.Description, vbInformation
End Function
Private Sub Form_Load()If ConnectToDatabase = False Then
MsgBox "Error!", vbInformation
Text1.Text = "false"
Else
Text1.Text = "true"
End IfEnd Sub
不知怎么回事,老是出错,输入ERROR。
ServerName = "(local)"我曾用127.0.0.1来连接也不行。
On Error GoTo Errhandler
Set g_conn = New ADODB.Connection
Dim ServerName As String, DBName As String, UserName As String, strPassword As String
ServerName = "(local)"
DBName = "meeting"
UserName = "sa"
strPassword = "sa"
With g_conn
.CursorLocation = adUseClient
.CommandTimeout = 100
.ConnectionString = "Provider=SQLOLEDB;Persist Security Info=true" & _
";User ID=" & UserName & ";Initial Catalog=" & DBName & _
"; Data Source=" & ServerName & ";Password=" & strPassword
.Open
End With
ConnectToDatabase = True
Exit Function
Errhandler
ConnectionToDatabase = False
MsgBox Err.Description, vbInformation
End Function
Private Sub Form_Load()If ConnectToDatabase = False Then
MsgBox "Error!", vbInformation
Text1.Text = "false"
Else
Text1.Text = "true"
End IfEnd Sub
不知怎么回事,老是出错,输入ERROR。
ServerName = "(local)"我曾用127.0.0.1来连接也不行。
确保有meeting数据库的存在,用户名sa和密码sa也是没错的。真不知哪里出了问题?
.
Errhandler:
.
.
cnn.ConnectionString = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Data Source=."