去哪了???VB高手

来源:百度知道 编辑:UC知道 时间:2024/07/07 06:40:51
请问我在窗体上放三个文本框(text1、text2、text3)、command1,我该怎么做才能把这三个文本框中的数据保存到SQL数据库,怎么做又能把它保存到Access数据库中呢?因本人初学VB时间不长,希望各位兄台能把代码写清,最好有源码,谢谢!

'首先要引入ADO2.5的库,你用控件也可以

Private Con As New ADODB.Connection '定义连接
Private Rs As New ADODB.Recordset '定义数据集
Private ConStr As String '定义变量

Private Sub Command1_Click()
If Text1.Text = "" Then
MsgBox "不能为空"
Text1.SetFocus
Exit Sub
End If

If Text2.Text = "" Then
MsgBox "不能为空"
Text2.SetFocus
Exit Sub
End If

If Text3.Text = "" Then
MsgBox "不能为空"
Text3.SetFocus
Exit Sub
End If
Rs.Open "select * from exam", Con, adOpenStatic, adLockOptimistic '打开数据库中的EXAM表
Rs.AddNew '添加
Rs("one") = Text1.Text
Rs("two") = Text2.Text
Rs("three") = Text3.Text
Rs.Update '更新
MsgBox "数据添加成功!"
Set Rs = Nothing '释放资源

End Sub

Private Sub Form_Load()
ConStr =