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()
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] '登录窗体名
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()