开发者社区> 科技小能手> 正文

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总
CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总
178 0
将图像转成HTML文件,VB.net源代码
上次发过一个软件,见下文http://www.cnblogs.com/aowind/archive/2005/03/05/113429.html其软件的功能就是将一个图像转成HTML文件,就是用一些自定义的数字通过不同的色彩来表现出这个图像其效果如下:经过小弟研究了一下,在vb.
719 0
RSS简易阅读器vb.net源代码
上次发了一个自己写的RSS阅读器的源代码http://www.cnblogs.com/aowind/archive/2005/03/16/119838.html这次打包发上来了上面还有用到的那个WEB浏览控件的COM组件http://files.
630 0
分享实录 | 阿里巴巴自研代码管理平台技术解密
近日,阿里巴巴旗下企业级一站式DevOps平台——阿里云·云效正式开启公测,为了让广大开发者更好地了解“新云效”,《云效说码》栏目特别策划了《为云研发而生 解密云效公测版专场》系列分享,邀请了5位阿里巴巴技术专家通过视频直播的方式与大家在线交流,分享云效产品的亮点和技术优势。
3340 0
Unity3D热更新之LuaFramework篇[09]--资源热更新与代码热更新的具体实现
Unity3D热更新之LuaFramework篇[09]--资源热更新与代码热更新的具体实现一、准备工作1、制作一个用于热更新的界面此前我制作了一个大厅界面,并且放置了两个按钮:”排行榜“和”商城“,排行榜按钮已经用于打开排行榜页面。
1184 0
软考之路--J2SE,从HelloWorld到传世代码
         “Hello World”程序指的是只在计算机屏幕上输出“Hello, World!”(意为“世界,你好!”)这行字符串的计算机程序。
955 0
动态执行 VB.NET 和 C# 代码
原文:动态执行 VB.NET 和 C# 代码 有时候我们需要尝试动态地与一些代码进行交互,而不是只能执行程序内已编死的代码,那该怎么办呢?我首先推荐各种脚本语言,如Javascript、Lua、Python等等,这些脚本语言有很多优秀的第三方类库,可以很方便的与 .NET 系统集成,让我们的程序中执行动态代码。
1135 0
23704
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载