求高手指导:VB窗口(以作个登陆界面为例)怎么样连接SQL server 数据库的表,求详细解说。
答案:2 悬赏:10 手机版
解决时间 2021-01-02 11:59
- 提问者网友:火车头
- 2021-01-01 12:35
求高手指导:VB窗口(以作个登陆界面为例)怎么样连接SQL server 数据库的表,求详细解说。
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-01-01 13:06
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
全部回答
- 1楼网友:风格不统一
- 2021-01-01 14:35
sql server首先要启动服务,然后需要通过网络连接来操作。最常用的是ado,dao等
下面是范例
这是ado方式进行连接。
须在工程=》引用里添加ado 2.5或者更高。
菜单 “工程”—“引用”中选择microsoft activex data objects 2.5 library组件
这是一个完整的过程。vb+sql2005调试通过。
dim conn as adodb.connection
dim rs as adodb.recordset
dim fld as adodb.field
dim sql as string
dim connstring as string
set conn = new adodb.connection
connstring = "provider=sqloledb;data source=数据库服务器ip或机器名;initial catalog=数据库名称;user id=数据库用户名;password=密码"
conn.connectionstring = connstring
conn.open
if err.number <> 0 then
msgbox "连接出现错误!"
end
exit function
end if
set rs = new adodb.recordset
rs.cursorlocation = aduseclient
rs.cursortype = adopenkeyset
sql = "select * from [数据表]"
rs.open sql, conn, adopendynamic, adlockoptimistic
if not rs.eof then
msgbox rs("字段")
end if
rs.close
conn.close
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯