天天看點

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)

    '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 Region

    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click

        Me.Close()

    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

        Catch

            MsgBox("無此用戶!", vbOKOnly + vbExclamation, "警告")

        End Try

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

    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()

    Private Sub Txt_Username_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Txt_Username.KeyDown

            Txt_pwd.Focus()

End Class

 本文轉自 simeon2005 51CTO部落格,原文連結:http://blog.51cto.com/simeon/94357