1、Vb6.0 连数据库1.1 ADO(ActiveX Data Object)连接方式在连接数据库前,首先要在 Visual Basic 6.0 菜单中“工程”“引用”中选择Microsoft ActiveX Data Objects 2.6 Library 和 Microsoft ActiveX Data Objects Recordset 2.6 Library 这两个组件。实例:以建立登陆系统为例,介绍无源数据库连接。在 Microsoft SQL Server 2000 中建立一个 Student 的数据库,在 Student 数据库中建立login_user(name,password
2、)的数据表,Microsoft SQL Server 服务器的名为data_server。代码如下:Option ExplicitPublic LoginSucceeded As Boolean引用 Microsoft ActiveX Data Objects 2.6 Library定义连接对象和记录集Dim conn As ADODB.ConnectionDim rs As ADODB.Recordset对取消按钮单击事件的响应Private Sub cmdCancel_Click()设置全局变量为 false不提示失败的登录LoginSucceeded = FalseMe.HideEnd
3、Sub对登录按钮单击事件的响应Private Sub cmdOK_Click()Set conn = New ADODB.ConnectionSet rs = New ADODB.Recordset建立无源数据库连接conn.ConnectionString=“driver=sql server;server=data_server;uid=sa;pwd=;Database=student“conn.ConnectionTimeout = 50conn.OpenDim str As String连接连接对象Set rs.ActiveConnection = conn设置游标类型rs.Curso
4、rType = adOpenDynamicIf Trim(txtUserName.Text = “) ThenMsgBox “用户名不能为空,请重新输入用户名!“, vbOKOnly + vbExclamation, “警告“txtUserName.SetFocusElse设置查询字符串str = “select * from login_user where name =“ uid=sa;pwd=;database=student“conn.OpenDim str As StringSet rs.ActiveConnection = connrs.CursorType = adOpenDynamicstr = “select * from login_user“rs.Open strtxtUserName.Text = rs.Fields(“name“)txtPassword.Text = rs.Fields(“password“)End Sub