今天将一个用word2003连接数据库的方法
首先打开word2003,新建一个word文档,依次打开视图-工具栏-选中visual basic,VB6的工具栏出现了.如下图所示
看见了吧,就是这个,有很多基本控件。我们点击最右边的锤子样的东西然后在列表选择Miscosoft ADO,设计个页面如下
这个是运行效果图,点击按钮进行查询,代码如下
Sub CommandButton1_Click()
Sub CommandButton1_Click()
Adodc1.ConnectionString = "Provider=SQLOLEDB;Initial Catalog=test ;Data Source=.\sql2k5;User ID=sa ;Password=boyuanec"
If TextBox1.Text = "" Then
MsgBox "用户ID不能为空", vbOKOnly + vbInformation, "提示"
Else
Adodc1.RecordSource = "select * from TUser where UserId=" & TextBox1.Text
Adodc1.Refresh
Label1.Caption = Adodc1.Recordset.Fields(1)
Label2.Caption = Adodc1.Recordset.Fields(2)
Label3.Caption = Adodc1.Recordset.Fields(3)
Label4.Caption = Adodc1.Recordset.Fields(4)
Label5.Caption = Adodc1.Recordset.Fields(5)
Image1.Picture = LoadPicture(Adodc1.Recordset.Fields(6))
Label6.Caption = Adodc1.Recordset.Fields(7)
End If
End Sub
Adodc1.ConnectionString = "Provider=SQLOLEDB;Initial Catalog=test ;Data Source=.\sql2k5;User ID=sa ;Password=boyuanec"
If TextBox1.Text = "" Then
MsgBox "用户ID不能为空", vbOKOnly + vbInformation, "提示"
Else
Adodc1.RecordSource = "select * from TUser where UserId=" & TextBox1.Text
Adodc1.Refresh
Label1.Caption = Adodc1.Recordset.Fields(1)
Label2.Caption = Adodc1.Recordset.Fields(2)
Label3.Caption = Adodc1.Recordset.Fields(3)
Label4.Caption = Adodc1.Recordset.Fields(4)
Label5.Caption = Adodc1.Recordset.Fields(5)
Image1.Picture = LoadPicture(Adodc1.Recordset.Fields(6))
Label6.Caption = Adodc1.Recordset.Fields(7)
End If
End Sub
看看数据库数据
成功了吧。
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/308863,如需转载请自行联系原作者