求高手指导:VB窗口(以作个登陆界面为例)怎么样连接SQL server 数据库的表,求详细解说。 5
1个回答
展开全部
1、新建表单,依次放入2个TextBox和2个CommandButton,分别命名为txtUsername,txtPassword,cmdOK,cmdCancel。
2、点击菜单:工程-引用,在可用的引用中找到Microsoft ActiveX Data Objects 2.1 Library,勾选上后确定。
3、然后粘贴以下代码:
Public sqlCON As New ADODB.Connection
Private Sub cmdCancel_Click()
sqlCON.Close
Unload Me
End
End Sub
Private Sub cmdOK_Click()
Dim passWord As String
Dim rs As New ADODB.Recordset
'检查正确的密码
userName = Trim(txtUserName.Text)
passWord = txtPassword.Text
'userlist为SQL数据库上管理程序用户和密码的表,表中至少包含username和password两个字段
Set rs = sqlCON.Execute("select username,password from userlist where username='" & Replace(userName, "'", "''") & "'")
If rs.EOF Then
MsgBox "无效的用户名!", Me.Caption
txtUserName.SetFocus
SendKeys "{Home}+{End}"
Exit Sub
End If
If Encrypt(passWord) = rs("password") Then
Screen.MousePointer = 11
frmMain.Show '调用登录成功后的主表单,请在主表单的Form_Load事件中,用Unload方法关闭本窗口
Screen.MousePointer = 0
Else
MsgBox "无效的密码!", Me.Caption
txtPassword.SetFocus
SendKeys "{Home}+{End}"
End If
End Sub
Private Sub Form_Load()
Dim dbPath As String
'请根据实际情况替代以下语句中的username(SQL数据库用户),mypassword(密码),mydatabase(默认数据库),myserver(SQL服务器)
dbPath = "Provider=Sqloledb.1;User Id=username;Pwd=mypassword;Initial Catalog=mydatabase;server=myserver"
sqlCON.Open "driver={SQL Server};dbq =" & dbPath
End Sub
2、点击菜单:工程-引用,在可用的引用中找到Microsoft ActiveX Data Objects 2.1 Library,勾选上后确定。
3、然后粘贴以下代码:
Public sqlCON As New ADODB.Connection
Private Sub cmdCancel_Click()
sqlCON.Close
Unload Me
End
End Sub
Private Sub cmdOK_Click()
Dim passWord As String
Dim rs As New ADODB.Recordset
'检查正确的密码
userName = Trim(txtUserName.Text)
passWord = txtPassword.Text
'userlist为SQL数据库上管理程序用户和密码的表,表中至少包含username和password两个字段
Set rs = sqlCON.Execute("select username,password from userlist where username='" & Replace(userName, "'", "''") & "'")
If rs.EOF Then
MsgBox "无效的用户名!", Me.Caption
txtUserName.SetFocus
SendKeys "{Home}+{End}"
Exit Sub
End If
If Encrypt(passWord) = rs("password") Then
Screen.MousePointer = 11
frmMain.Show '调用登录成功后的主表单,请在主表单的Form_Load事件中,用Unload方法关闭本窗口
Screen.MousePointer = 0
Else
MsgBox "无效的密码!", Me.Caption
txtPassword.SetFocus
SendKeys "{Home}+{End}"
End If
End Sub
Private Sub Form_Load()
Dim dbPath As String
'请根据实际情况替代以下语句中的username(SQL数据库用户),mypassword(密码),mydatabase(默认数据库),myserver(SQL服务器)
dbPath = "Provider=Sqloledb.1;User Id=username;Pwd=mypassword;Initial Catalog=mydatabase;server=myserver"
sqlCON.Open "driver={SQL Server};dbq =" & dbPath
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询