验证登陆用户名与密码

kuanglhx 2007-05-23 09:59:36
我在自己的机子的SQLserver上建了个 student的数据库,在vb.net的 loginfrm中有两个textbox 分别是txtName,txtPwd.还有个button。我希望当我点击按扭,能验证用户名和密码是否正确。希望各位老大回消息是尽量写详细点。
在此,小弟先谢过了。
...全文
473 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuanglhx 2007-05-26
  • 打赏
  • 举报
回复
恩 太感谢你了
dengxiaobo 2007-05-26
  • 打赏
  • 举报
回复

con = New SqlConnection("server=.;database=你的数据库;uid=sa;pwd=;")


优化一下:
If datar("UserAccount") = txtname.Text And datar("UserPwd") = txtpassword.Text Then
' 登录成功
Dim f As New frmMain 'frmmain为登录成功进入的窗体,需另外添加
f.Show()
Me.Hide()

End If

改为
Dim f As New frmMain 'frmmain为登录成功进入的窗体,需另外添加
f.Show()
Me.Hide()
kuanglhx 2007-05-26
  • 打赏
  • 举报
回复
但我做出来后,运行在con.open()这就停了,弹出一个:{未处理的“System.NullReferenceException”类型的异常出现在 WindowsApplication1.exe 中。

其他信息: 未将对象引用设置到对象的实例}的对话框。请问是不是我的sql语句中出错。
老大谢谢你了,老是麻烦你真不意思
dengxiaobo 2007-05-26
  • 打赏
  • 举报
回复
Imports System.Data
Imports System.Data.SqlClient
Public Class frmLogin
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtname As System.Windows.Forms.TextBox
Friend WithEvents txtpassword As System.Windows.Forms.TextBox
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents btnCancel As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtname = New System.Windows.Forms.TextBox
Me.txtpassword = New System.Windows.Forms.TextBox
Me.btnOK = New System.Windows.Forms.Button
Me.btnCancel = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 48)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 0
Me.Label1.Text = "用户名"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 96)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 1
Me.Label2.Text = "用户密码"
'
'txtname
'
Me.txtname.Location = New System.Drawing.Point(144, 48)
Me.txtname.Name = "txtname"
Me.txtname.TabIndex = 2
Me.txtname.Text = ""
'
'txtpassword
'
Me.txtpassword.Location = New System.Drawing.Point(144, 96)
Me.txtpassword.Name = "txtpassword"
Me.txtpassword.TabIndex = 3
Me.txtpassword.Text = ""
'
'btnOK
'
Me.btnOK.Location = New System.Drawing.Point(56, 152)
Me.btnOK.Name = "btnOK"
Me.btnOK.TabIndex = 4
Me.btnOK.Text = "确定"
'
'btnCancel
'
Me.btnCancel.Location = New System.Drawing.Point(152, 152)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.TabIndex = 5
Me.btnCancel.Text = "取消"
'
'frmLogin
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.btnOK)
Me.Controls.Add(Me.txtpassword)
Me.Controls.Add(Me.txtname)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Name = "frmLogin"
Me.Text = "frmLogin"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim con = New SqlConnection ("server=.;database=clientMgrNet;uid=sa;pwd=;" )
'以上为创建连接
con.Open() '打开连接数据库
Dim datar As SqlDataReader '创建一个datareader对象,此对象仅返回一个只读的,仅向前的数据流
Dim cmd As SqlCommand '定义一个command对象
'定义SQL语句
Dim Sqlstr As String = "select UserAccount,UserPwd from Users where UserAccount='" & txtname.Text & "' and UserPwd='" & txtpassword.Text & "'"
cmd = New SqlCommand(Sqlstr, con)
datar = cmd.ExecuteReader
con.Close() '关闭连接数据库
If datar.Read Then '判断内存中是否有记录
If datar("UserAccount") = txtname.Text And datar("UserPwd") = txtpassword.Text Then
' 登录成功
Dim f As New frmMain 'frmmain为登录成功进入的窗体,需另外添加
f.Show()
Me.Hide()

End If
Else
'登录失败
MsgBox("用户名或者密码不正确","提示信息")
txtname.Text = "" '清空输入
txtpassword.Text = ""
txtname.focus() '获得焦点
End If
End Sub

Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close() '关闭登录窗体
End Sub
End Class

'注:#Region " Windows 窗体设计器生成的代码 " 和 #End Region 为自动生成
'需设置[项目]--[项目名属性]-[项目名属性页]--启动对象--为[frmlogin] '登录窗体名
kuanglhx 2007-05-25
  • 打赏
  • 举报
回复
老大在详细一点就给满分了哦, 最好多加点文字啊, 新手上路,多多原谅啊
dengxiaobo 2007-05-24
  • 打赏
  • 举报
回复
应该是 connString = "server=.;database=student;uid=sa;pwd=;"
dengxiaobo 2007-05-24
  • 打赏
  • 举报
回复
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim connString As String
connString = "server=.;database=clientMgrNet;uid=sa;pwd=;"
'Dim dataAdapter As SqlDataAdapter
Dim datar As SqlDataReader
Dim cmd As SqlCommand
Dim con As SqlConnection
'Dim ds As DataSet

Dim Sqlstr As String = "select UserAccount,UserPwd from Users where UserAccount='" & txtname.Text & "' and UserPwd='" & txtpwd.Text & "'"
con = New SqlConnection(connString)
con.Open()
cmd = New SqlCommand(Sqlstr, con)
'ds = New DataSet
datar = cmd.ExecuteReader
If datar.Read Then
If datar("UserAccount") = txtname.Text And datar("UserPwd") = txtpwd.Text Then
' 登录成功
Dim f As New frmMain 'frmmain为登录成功进入的窗体
f.Show()
Me.Hide()

End If
Else
MsgBox("用户名或者密码不正确")
End If
End Sub

End Class

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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