'生成连接字符串
Private Sub CmdOk_Click()
on error goto err
If Option1.Value = True Then '远程数据库路径配置
If Trim(TxtIpAddress.Text) = "" Then
MsgBox "IP地址不能为空!", vbInformation
TxtIpAddress.SetFocus
Exit Sub
End If
End If
If Trim(TxtDatabaseName.Text) = "" Then
MsgBox "数据库名称不能为空!", vbInformation
TxtDatabaseName.SetFocus
Exit Sub
End If
If Trim(TxtDataBaseUserName.Text) = "" Then
MsgBox "用户名称不能为空!", vbInformation
TxtDataBaseUserName.SetFocus
Exit Sub
End If
StrSql = "Provider=SQLOLEDB.1;Persist Security Info=True;"
StrSql = StrSql & "User ID=" & Trim(TxtDataBaseUserName.Text)
StrSql = StrSql & ";Initial Catalog=" & Trim(TxtDatabaseName.Text)
If Option1.Value = True Then '远程数据库
StrSql = StrSql & ";Data Source=" & Trim(TxtIpAddress.Text)
End If
If Option2.Value = True Then '本地数据库
StrSql = StrSql & ";Data Source=(local)"
End If
StrSql = StrSql & ";password=" & Trim(TxtDataBasePassword.Text)
strPath = Trim(StrSql)
Open App.Path & "\DataBasePath.txt" For Output As #1 '将字符串写入文本文件
Print #1, strPath
Close #1
Command2.Enabled = True
Exit Sub
err:
MsgBox "非法操作!", vbInformation + vbOKOnly, "系统提示!"Exit SubEnd Sub'连接数据库Private Sub Command2_Click()
On Error GoTo ToExit '打开错误陷阱
'------------------------------------------------ Dim Textline As String
If Dir(App.Path & "\DataBasePath.txt") = "" Then
Open App.Path & "\DataBasePath.txt" For Output As #1
Close #1
End If
Open App.Path & "\DataBasePath.txt" For Input As 1# '取出连接字符串 Do While Not EOF(1)
Line Input #1, Textline
Loop strPath = Trim(Textline)
Close #1
cn.Open (strPath)
PopMessageBox "成功提示:连接数据库成功", "3", "4"
cn.Close
Command2.Enabled = False
'------------------------------------------------ Exit Sub '----------------
ToExit:
PopMessageBox "错误提示:连接数据库不成功可能原因;1、数据库不存在;2、用户名和密码错误", "3", "4"
End Sub问题:如果我选择IP地址连接的话,那会在有防火墙时没法连成功的现象(即使IP是本机的也会)。我想能不能不管防火墙有没有开衷启都能正常连接上,要如何修改我的语句呢???
Private Sub CmdOk_Click()
on error goto err
If Option1.Value = True Then '远程数据库路径配置
If Trim(TxtIpAddress.Text) = "" Then
MsgBox "IP地址不能为空!", vbInformation
TxtIpAddress.SetFocus
Exit Sub
End If
End If
If Trim(TxtDatabaseName.Text) = "" Then
MsgBox "数据库名称不能为空!", vbInformation
TxtDatabaseName.SetFocus
Exit Sub
End If
If Trim(TxtDataBaseUserName.Text) = "" Then
MsgBox "用户名称不能为空!", vbInformation
TxtDataBaseUserName.SetFocus
Exit Sub
End If
StrSql = "Provider=SQLOLEDB.1;Persist Security Info=True;"
StrSql = StrSql & "User ID=" & Trim(TxtDataBaseUserName.Text)
StrSql = StrSql & ";Initial Catalog=" & Trim(TxtDatabaseName.Text)
If Option1.Value = True Then '远程数据库
StrSql = StrSql & ";Data Source=" & Trim(TxtIpAddress.Text)
End If
If Option2.Value = True Then '本地数据库
StrSql = StrSql & ";Data Source=(local)"
End If
StrSql = StrSql & ";password=" & Trim(TxtDataBasePassword.Text)
strPath = Trim(StrSql)
Open App.Path & "\DataBasePath.txt" For Output As #1 '将字符串写入文本文件
Print #1, strPath
Close #1
Command2.Enabled = True
Exit Sub
err:
MsgBox "非法操作!", vbInformation + vbOKOnly, "系统提示!"Exit SubEnd Sub'连接数据库Private Sub Command2_Click()
On Error GoTo ToExit '打开错误陷阱
'------------------------------------------------ Dim Textline As String
If Dir(App.Path & "\DataBasePath.txt") = "" Then
Open App.Path & "\DataBasePath.txt" For Output As #1
Close #1
End If
Open App.Path & "\DataBasePath.txt" For Input As 1# '取出连接字符串 Do While Not EOF(1)
Line Input #1, Textline
Loop strPath = Trim(Textline)
Close #1
cn.Open (strPath)
PopMessageBox "成功提示:连接数据库成功", "3", "4"
cn.Close
Command2.Enabled = False
'------------------------------------------------ Exit Sub '----------------
ToExit:
PopMessageBox "错误提示:连接数据库不成功可能原因;1、数据库不存在;2、用户名和密码错误", "3", "4"
End Sub问题:如果我选择IP地址连接的话,那会在有防火墙时没法连成功的现象(即使IP是本机的也会)。我想能不能不管防火墙有没有开衷启都能正常连接上,要如何修改我的语句呢???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货