vb.net 登录access源代码【原创】

 
在窗体中加入两个txt框,两个按钮。界面设计如上图所示。
数据库中设计access表,adminuser,表中创建username、passwrod两个文本字段。access文件名称为my.mdb.其源代码如下:
Imports System.Data
Imports System.Data.OleDb
Public Class LoginFrm
    Inherits System.Windows.Forms.Form
    Public ADOcmd As OleDbDataAdapter
    Public ds As DataSet = New DataSet
    Public mytable As Data.DataTable
    Public myrow As Data.DataRow
    Public rownumber As Integer
    Public searchsql As String
    Public cmd As OleDbCommandBuilder
    Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)
        ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath() & "\my.mdb")
        ADOcmd.Fill(ds, table)
        mytable = ds.Tables.Item(0)
        rownumber = 0
        myrow = mytable.Rows.Item(rownumber)
    End Function
#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 btn_ok As System.Windows.Forms.Button
    Friend WithEvents btn_cancel As System.Windows.Forms.Button
    Friend WithEvents Txt_Username As System.Windows.Forms.TextBox
    Friend WithEvents Txt_pwd As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Txt_Username = New System.Windows.Forms.TextBox
        Me.Txt_pwd = New System.Windows.Forms.TextBox
        Me.btn_ok = New System.Windows.Forms.Button
        Me.btn_cancel = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(48, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(54, 17)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "用户名:"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(48, 64)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(54, 17)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "密  码:"
        '
        'Txt_Username
        '
        Me.Txt_Username.Location = New System.Drawing.Point(112, 32)
        Me.Txt_Username.Name = "Txt_Username"
        Me.Txt_Username.Size = New System.Drawing.Size(96, 21)
        Me.Txt_Username.TabIndex = 2
        Me.Txt_Username.Text = ""
        '
        'Txt_pwd
        '
        Me.Txt_pwd.Location = New System.Drawing.Point(112, 64)
        Me.Txt_pwd.Name = "Txt_pwd"
        Me.Txt_pwd.PasswordChar = Microsoft.VisualBasic.ChrW(42)
        Me.Txt_pwd.Size = New System.Drawing.Size(96, 21)
        Me.Txt_pwd.TabIndex = 3
        Me.Txt_pwd.Text = ""
        '
        'btn_ok
        '
        Me.btn_ok.Location = New System.Drawing.Point(64, 104)
        Me.btn_ok.Name = "btn_ok"
        Me.btn_ok.Size = New System.Drawing.Size(48, 24)
        Me.btn_ok.TabIndex = 4
        Me.btn_ok.Text = "登录"
        '
        'btn_cancel
        '
        Me.btn_cancel.Location = New System.Drawing.Point(160, 104)
        Me.btn_cancel.Name = "btn_cancel"
        Me.btn_cancel.Size = New System.Drawing.Size(48, 24)
        Me.btn_cancel.TabIndex = 5
        Me.btn_cancel.Text = "关闭"
        '
        'LoginFrm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(296, 158)
        Me.Controls.Add(Me.btn_cancel)
        Me.Controls.Add(Me.btn_ok)
        Me.Controls.Add(Me.Txt_pwd)
        Me.Controls.Add(Me.Txt_Username)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "LoginFrm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "登录"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
        Me.Close()
    End Sub
    Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
        '定义 
        Dim tablename As String
        tablename = "adminuser"
        Dim SearchSQL As String
        SearchSQL = "select username,password from adminuser where username='" & Txt_Username.Text & "'"
        Try
            ExecuteSQL(SearchSQL, tablename)
            If myrow.Item(1) = Txt_pwd.Text Then
                MsgBox("成功登陆")
                Dim FormMain As New FormMain
                FormMain.Show()
                Me.Hide()
            Else
                MsgBox("密码不正确!", vbOKOnly + vbExclamation, "警告")
                Exit Sub
            End If
        Catch
            MsgBox("無此用戶!", vbOKOnly + vbExclamation, "警告")
        End Try
    End Sub
    Private Sub LoginFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub Txt_pwd_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Txt_pwd.KeyDown
        If e.KeyCode = Keys.Enter Then
            btn_ok.Visible = True
            btn_ok.Focus()
        End If
    End Sub
    Private Sub Txt_Username_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Txt_Username.KeyDown
        If e.KeyCode = Keys.Enter Then
            Txt_pwd.Focus()
        End If
    End Sub
End Class

 本文转自 simeon2005 51CTO博客,原文链接:http://blog.51cto.com/simeon/94357

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值