① 怎么用vb做一个连接sql的用户登录界面
思路简单,具体操作参考相资料:
1、建立数据库
2、用相关控件连接数据库
3、设计登录界面
4、写代码:判断用户输入的用户名和密码是否与数据库里记录的一致,是则同意登录,打开程序主窗口,不是则不同意登录,继续重新登录或退出程序。
② 如何用VB连接SQL数据库做登录
一、界面设计
各控件名称属性分别为:label1 、text1 、label2、text2、commandok、cmdcancel
二、代码设计如下:
1、首先添加一个模块,写上以下通用声明和Sub main():
Public conn As ADODB.Connection '通用(声明)
Sub main()
Set conn = New ADODB.Connection '通用(main)
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;" _
+ "User ID=sa;password=123;Initial Catalog=denglu;Data Source=127.0.0.1" '连接数据库代码
conn.Open
frmLogin.Show '首先显示登录界面。也可以在工程属性中设置启动对象为Sub main()或者frmlogin窗体
End Sub
2、在Frmlogin 代码窗口,为cmdok控件写以下代码:
Private Sub cmdok_Click()
If text1.Text = "" Then
MsgBox "用户名不能为空!", vbOKOnly + vbInformation, "友情提示"
text1.SetFocus
Exit Sub '若用户名文本框内为空,则出现提示框
End If
If text2.Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "友情提示"
text2.SetFocus
Exit Sub '若输入密码文本框为空,也出现提示框
End If
Dim strSQl As String
strSQl = "select * from User1 where username='" & Trim$(text1.Text) & "' and pwd='" & Trim$(text2.Text) & "' "
3、书写SQL代码,查询User1表中是否存在窗体中用户输入的信息。
Dim str As New ADODB.Recordset
Set str = New ADODB.Recordset
str.CursorLocation = adUseClient
str.Open strSQl, conn, adOpenStatic, adLockReadOnly
With str
If .State = adStateOpen Then .Close
.Open strSQl
If .EOF Then
Try_times = Try_times + 1
If Try_times >= 3 Then
MsgBox "您已连续三次输入错误,系统将自动关闭", vbOKOnly + vbCritical, "警告"
Unload Me '若用户连续输入3次错误密码,则系统关闭
Else
MsgBox "对不起,用户名不存在或密码错误 !", vbOKOnly + vbQuestion, "警告"
text1.SetFocus
text1.Text = ""
text2.Text = ""
End If
Else
Unload Me '若登录成功,则隐藏当前窗体
Form2.Show '然后显示Form窗体
End If
End With
End Sub
Private Sub cmdCancel_Click()
End '若单击Cmdcel按钮,则结束应用程序
End Sub
4、运行中存在的问题:
代码中有Dim conn As adodb.connection,运行时显示"用户定义类型未定义"
解决方法:点击“工程”--“引用”找到“Microsoft ActiveX Data Object 2.6”
③ VB+SQL怎么编写一个登陆界面啊
OptionExplicit
Dimerrpass%,password$
DimfinserAsBoolean
PublicFunctionSearchdata(用户名AsString,密码AsString)AsBoolean
Searchdata=False
Adodc1.Recordset.MoveFirst
Adodc1.Recordset.Find"用户名='"&用户名&"'"
IfNotAdodc1.Recordset.EOFThen
IfTrim(Adodc1.Recordset.Fields("密码"))=密码ThenSearchdata=True
EndIf
EndFunction
PrivateSubComm11_Click()
finser=Searchdata(Trim(Text1.Text),Trim(Text2.Text))
IffinserThen
MsgBox"登录成功!",vbOKOnly,"密码登录"
Form1.Show
Me.Show
Else
errpass=errpass+1
Iferrpass>=3Then
MsgBox"对不起,您没有任何权限登录使用本系统",vbCritical,"密码登录"
Comm11.Enabled=False
Else
MsgBox"用户名或密码错误,请重新输入",vbCritical,"密码登录"
Text1.SetFocus
EndIf
EndIf
EndSub
PrivateSubComm22_Click()
UnloadMe
EndSub
PrivateSubForm_Load()
Adodc1.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&App.Path&".yang_75.mdb"
Adodc1.RecordSource="select*frommei"
Adodc1.Refresh
'Text2.SetFocus
Text1.Text=""
Text2.Text=""
Text2.PasswordChar="*"
EndSub
PrivateSubForm_Unload(CancelAsInteger)
Adodc1.Recordset.Close
End
EndSub
④ 如何用VB连接SQL数据库做登录
使用ADODC控件,设置adodc控件的ConnectionString 属性为"Provider=SQLOLEDB.1;Password=数据库登录密码;Persist Security Info=True;User ID=数据库登录名;Initial Catalog=数据库名称;Data Source=服务器名称或IP地址" 然后设置Adodc1.CommandType=表名或者SQL数据源 Adodc1.RecordSource=表名或者SQL语句 adodc1.Refresh 以上就已经连接上一个指定的数据源了,接下来就可以进行各种操作啦
⑤ vb连接SQL数据库后制作一个登陆界面。
Option Explicit
Private Function Selectsql(SQL As String) As ADODB.Recordset '返回ADODB.Recordset对象
Dim ConnStr As String
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set Conn = New ADODB.Connection
'On Error GoTo MyErr:
ConnStr = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=登录数据库用户名(默认为sa);Password=登录数据库密码;Initial Catalog=数据库名;Data Source=服务器名(默认为:MERRYCHINA)" '这是连接SQL数据库的语句
Conn.Open ConnStr
rs.CursorLocation = adUseClient
rs.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set Selectsql = rs
'Exit Function
'MyErr:
'Set rs = Nothing
'Set Conn = Nothing '释放相关的系统资源
'MsgBox Err.Description, vbInformation, "系统提示" '显示出错信息
End Function
Private Sub Form_Load()
Dim SQL As String
Dim rs As ADODB.Recordset
Dim X As Long
On Error GoTo Err_box
SQL = " select * from 用户表"
Set rs = Selectsql(SQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
For X = 1 To rs.RecordCount
Combo1.AddItem rs.Fields("用户名").Value
rs.MoveNext
Next X
Combo1.ListIndex = 0
End If
rs.Close
Exit Sub
Err_box:
End Sub
Private Sub Command1_Click()
Dim SQL As String
Dim rs As ADODB.Recordset
If Text1.Text = "" Then
MsgBox "请输入口令!", 16
Text1.SetFocus
Exit Sub
End If
If Combo1.Text = "" Then
MsgBox "请选择登录用户!", 16
Combo1.SetFocus
Exit Sub
End If
SQL = "SELECT * FROM 用户表 WHERE 用户名='" & Combo1.Text & "' AND 密码='" & Text1.Text & "' "
Set rs = Selectsql(SQL)
If rs.RecordCount > 0 Then
Form1.Show '想要打开的主窗体
MsgBox "恭喜兄弟,登录成功!", 64, "提示"
Unload Me
Else
MsgBox "口令不对,请重新输入!", 16, "提示"
Text1.SetFocus
End If
End Sub
'**********************************************************************
'说明:1) 在工程中引用Microsoft ActiveX Data Objects 2.8 Library ,其它版本也行如:2.0
' 2) 在窗体中加Texe1.text(文本框控件),Combo1.text(组合框控件),Command1(命令按钮)各一个
' 3) 在SQL Server2000中创建数据库,新建表"用户表",表中包含"ID,姓名,密码"等字段,然后将以上代码复制,OK搞定
4) 以上方式无需加载ADO控件,方便!
>密码和帐号对的时候跳转到form2,在form2的的文本框text1中出现text1的帐号。
这个你只要设置一个全局变量,然后对的时候把帐号放到全局变量里,然后在打开form2时显示这个全局变量到text1里就行了。
⑥ 如何用VB连接SQL数据库做登录
dim i as Integer
'用户密码检查
Public Sub ck_userpass()
Dim ck_m As New SqlCommand
ck_m.Connection = sql_conn
ck_m.CommandText = "select count(uname) from jc_user where uname=@uname and upass=@upass"
ck_m.Parameters.AddWithValue("@uname", txtuser.Text)
ck_m.Parameters.AddWithValue("@upass", txtpass.Text)
sql_conn.Open()
Dim obj1 As Object = ck_m.ExecuteScalar
sql_conn.Close()
i = CType(obj1, Integer)
End Sub
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
ck_userpass()
If i = 0 Then
MessageBox.Show("用户名或者密码错误")
txtpass.Text = Nothing
txtpass.Focus()
Else
xx.show()
MessageBox.Show("验证成功")
Me.Close()
End If
End Sub
⑦ vb和sql做系统登录页面
假设表bookbuyer在数据库book中,bookbuyer表中的字段username和password分别对应用户名和密码
在工程中添加两个窗体(form1和form2),在form1上画上text1和text2还有一个Commandbutton,点击菜单栏上的工程>>>引用>>>勾选Microsoft Activex Data Objects 2.6 Library,再点击确定。在Command1的Click事件中添加如下代码。
Dim conn As New ADODB.Connection
cnstr = "Provider=SQLOLEDB;Data Source=你的IP地址或计算机名;DATABASE=book;UID=你的数sql数据库用户名;pwd=sql数据库密码"
conn.Open cnstr
sQl1 = "select * from book where username='" & Text1.Text & "'"
sQL2 = "select * from book where password='" & Text2.Text & "'"
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset
rs1.Open sQl1, conn, 1, 3
rs2.Open sQL2, conn, 1, 3
If rs1.EOF = True Or rs2.EOF = True Then
MsgBox "密码或用户名错误!", vbExclamation, "登陆"
Else
Form2.Show
End If
rs1.Close
rs2.Close
这是我自己设计时使用的登录代码,有什么不足的地方。。。。。。
⑧ 如何用VB连接SQL数据库做登录
VB连接SQL数据库,可通过ADO数据对象和SQL结构化查询语言实现。
ADO (ActiveX 数据对象), 这项新的数据访问技术的特性包括:更简单的对象模型;与其它 Microsoft 和非 Microsoft
的技术更好的集成;为本地和远程数据数据提供的通用接口;可远程访问的和断开的记录集;用户可访问的数据绑定接口;以及层次结构的记录集。结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。
代码示例:
Private Sub XPButton1_Click()
On Error GoTo finish '防错代码,防止意外而导致的退出
sql = "select * from 用户管理 where 用户名='" & Text1.Text & "' and 密码='" & Text2.Text & "'"
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=登录数据库用户名(默认为sa);Password=登录数据库密码;Initial Catalog=数据库名;Data Source=服务器名(默认为:MERRYCHINA)" '这是连接SQL数据库的语句
cn.Open
rs.CursorLocation = adUseClient
rs.Open sql, cn, adOpenDynamic, adLockOptimistic
'以上使用最通用的方法来查询数据库中是否有匹配的记录
If rs.EOF = True Then '如果没有记录则说明用户或密码为错误的
If pnum < 2 Then 'pnum就是密码验证次数,当次数超过3次,系统会自动保护退出
pnum = pnum + 1
MsgBox "用户名或密码错误!", vbInformation, "错误次数:" & pnum
rs.Close
cn.Close
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
Exit Sub
Else
MsgBox "用户名或密码错误超过三次,系统会自动退出", vbInformation, "提示"
End
End If
Else
loginname = rs.Fields(0)
Form1.Show
rs.Close
cn.Close
End If
Exit Sub
finish:
MsgBox Err.Description
rs.Close
cn.Close
End Sub
注意cn.ConnectionString此句需依据实际的SQL数据库建立连接获得的字符串。