麻烦各位看一下,这是我自己写的代码,为什么连接数据库老有问题

zhang_765 2012-05-08 10:18:04
麻烦各位看一下,这是我自己写的代码,为什么连接数据库老有问题

Dim str As String
Dim rst As New ADODB.Recordset
Dim cn As New ADODB.Connection


Private Sub Command1_Click()

Dim conlogin As New ADODB.Connection
Dim rstlogin As New ADODB.Recordset


If Len(Trim(txtUserId.Text)) = 0 Then
MsgBox "用户账号为空,请重新输入!!", vbOKOnly
Exit Sub
End If

If Len(Trim(txtUserName.Text)) = 0 Then
MsgBox "用户姓名为空,请重新输入!!", vbOKOnly
Exit Sub
End If

If Len(Trim(txtUserPsw.Text)) = 0 Then
MsgBox "用户密码为空,请重新输入!!", vbOKOnly
End If

With conlogin
If .State = adStateOpen Then
.Close

End If
.Provider = "SQLOLEDB.1"
.ConnectionString = "User ID=sa;Password=xczhang;Initial Catalog=GaoSuGL;Data Source="
.Open
End With



End Sub

Private Sub txtUserName_Click()
str = "select * from UserLogin where UserId= '" & Text & "' "
Adodc1.RecordSource = str
Adodc1.Refresh

End Sub

小弟不胜感激!
...全文
1106 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Q2565093493 2012-10-17
  • 打赏
  • 举报
回复
用 adodc 控件 动态数据源 简单一点
熊孩子开学喽 2012-10-11
  • 打赏
  • 举报
回复
最最最最简单的方法:

1: 添加Adodc控件
2: 用这控件联数据库
3: 查看控件的"连接字符串"

啥也不用记. 复制好连接字符串就可以把控件给扔了.
worldy 2012-05-09
  • 打赏
  • 举报
回复
Dim conlogin As New ADODB.Connection
Dim rstlogin As New ADODB.Recordset

定义的局部变量,当函数退出的时候,就背卸载了,command1事件上做了一件什么意义都没有的事情,可以将这两句删除,直接使用模块变量cn和rs

txtUserName_Click中使用了adodc控件,和你几个过程中定义的连接或记录集没有任何关系,不知道你adodc控件中是否指定了连接?一般,如果使用adodc就不要使用代码定义连接,
jarvis_hjm 2012-05-09
  • 打赏
  • 举报
回复
连接字符串不完整
zhang_765 2012-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
连接字符串不完整
[/Quote]
哦,多谢。
zhang_765 2012-05-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
Dim conlogin As New ADODB.Connection
Dim rstlogin As New ADODB.Recordset

定义的局部变量,当函数退出的时候,就背卸载了,command1事件上做了一件什么意义都没有的事情,可以将这两句删除,直接使用模块变量cn和rs

txtUserName_Click中使用了adodc控件,和你几个过程中定义的连接或记录集没有任……
[/Quote]

多谢,我好好看下。
zhang_765 2012-05-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
.ConnectionString = "User ID=sa;Password=xczhang;Initial Catalog=GaoSuGL;Data Source=计算机名或IP地址"
[/Quote]

多谢前辈,我试一下。
饮水需思源 2012-05-08
  • 打赏
  • 举报
回复
.ConnectionString = "User ID=sa;Password=xczhang;Initial Catalog=GaoSuGL;Data Source=计算机名或IP地址"

1,066

社区成员

发帖
与我相关
我的任务
社区描述
VB 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧