我这段VB代码哪里错了。请高手指教

来源:百度知道 编辑:UC知道 时间:2024/09/20 12:12:55
Dim a As String
Dim b As Integer

Sub Main()
On Error GoTo On_error
Set con = New ADODB.Connection
con.ConnectionString = ("Provider=SQLOLEDB;User ID=" & Text2.Text & ";PWD=" & Text3.Text & ";Initial Catalog=" & Text4.Text & ";Data Source=" & Text1.Text)
Connection.state
con.Open
If Err.Number = 0 Then
b = 1
Else
b = 2
End If
Debug.Print b
Exit Sub
On_error:
End Sub

Private Sub Command1_Click()

If Text1.Text = "" Then

MsgBox ("服务器名不能为空")

Else

If Text2.Text = "" Then

MsgBox "用户名不能为空"

Else
If Text4.Text = "" Then

MsgBox "数据库不能为空"
Else
If b = 1 Then
MsgBox "数据库连接成功"
Else
MsgBox "数据库连接失败"
End If
End If
End If
End If

End Sub

可能是数据库服务器连接方式引起,你尝试改变一下连接字符串:
con.ConnectionString = ("Provider=SQLOLEDB;User ID=" & Text2.Text & ";PWD=" & Text3.Text & ";Initial Catalog=" & Text4.Text & ";Data Source=" & Text1.Text)
改为:
con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & Text2.Text & ";Initial Catalog=" & Text4.Text & ";Data Source=" & Text1.Text & ";pwd=warom=" & Text3.Text & ";Network Library=dbmssocn"
或者以Windows身份验证方式:
con.ConnectionString = "Provider=SQLOLEDB.1;Network Library=dbmssocn;Persist Security Info=False;Initial Catalog=" & Text4.Text & ";Data Source=" & Text1.Text & ";Integrated Security=SSPI"