VB中怎样用ADO与SQL数据库
VB连接SQL数据库示例
作者:hdf007 日期:2005-04-17 13:10:56
本文出自网易虚拟社区( ),如需转载,请联系原作者或网易
先建一个工程,建立一个form1,form1中的代码如下:
Dim rsNew As ADODB。 Recordset
Dim CnNew As ADODB。Connection
Dim wsh As New WshShell
Dim serverlink As String
Private Sub Command1_Click()
Dim Mag As String
On Error GoTo strErrMag
Set CnNew =...全部
VB连接SQL数据库示例
作者:hdf007 日期:2005-04-17 13:10:56
本文出自网易虚拟社区( ),如需转载,请联系原作者或网易
先建一个工程,建立一个form1,form1中的代码如下:
Dim rsNew As ADODB。
Recordset
Dim CnNew As ADODB。Connection
Dim wsh As New WshShell
Dim serverlink As String
Private Sub Command1_Click()
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB。
Connection
Set wsh = New WshShell
Text1 = wsh。RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\SERVERNAME")
Text3 = wsh。
RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\PWD")
Text2 = wsh。RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\USENAME")
serverlink = "Provider=SQLOLEDB。
1;Password=" + Trim(Text3。Text) + ";Persist Security Info=True;User ID=" + Trim(Text2。Text) + ";Initial Catalog=master;Data Source=" + Trim(Text1。
Text)
CnNew。Open serverlink
Exit Sub
strErrMag:
Mag = "数据库连接错误,在下列的窗体中请输入正确的信息!"
MsgBox Mag, vbOKOnly, "错误:数据库连接"
Me。
Enabled = False
Load Form2
Form2。Show
End Sub
再建一个form2,form2的代码如下:
Dim rsNew As ADODB。Recordset
Dim CnNew As ADODB。
Connection
Dim wsh As New WshShell
Dim serverlink As String
Private Sub Command1_Click()
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB。
Connection
Set wsh = New WshShell
serverlink = "Provider=SQLOLEDB。1;Password=" + Trim(Text3。Text) + ";Persist Security Info=True;User ID=" + Trim(Text2。
Text) + ";Initial Catalog=master;Data Source=" + Trim(Text1。Text)
CnNew。Open serverlink
MsgBox ("数据库连接成功!"), vbInformation + vbOKOnly, "提示信息"
Set CnNew = Nothing
wsh。
RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\SERVERNAME", Trim(Text1)
wsh。RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\PWD", Trim(Text3)
wsh。
RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\ZJRB\USENAME", Trim(Text2)
Form1。
Enabled = True
Unload Me
Exit Sub
strErrMag:
Mag = "数据库连接错误,在下列的窗体中请输入正确的信息!"
MsgBox Mag, vbOKOnly, "错误:数据库连接"
End Sub
。收起