| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2116 人关注过本帖
标题:~~~如何在VB中实现与数据库相联系的登陆窗口~~~
只看楼主 加入收藏
雪中浪人
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2005-7-8
收藏
 问题点数:0 回复次数:4 
~~~如何在VB中实现与数据库相联系的登陆窗口~~~
请问如何在VB中实现与数据库相联系的登陆窗口?
并且用户与密码等资料存储在数据库文件中(或记事本文件中)?

请求高手给段代码作示范!!!
搜索更多相关主题的帖子: VB中 数据库 窗口 登陆 联系 
2005-07-09 01:59
雪中浪人
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2005-7-8
收藏
得分:0 
自顶!!

2005-07-09 12:36
JJM
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2005-7-9
收藏
得分:0 

先建一个类(database) Imports System.Data Imports System.Data.SqlClient Imports System.ComponentModel Public Class database '实现接口IDisposable Implements IDisposable '数据库连接对象 Private SqlConn As SqlConnection Public Shared sConn As String = "Persist Security Info=False;" _ + "Integrated Security=SSPI;database=HICAP;" _ + "server=zg10;Connect Timeout=30" Public Sub Dispose() Implements System.IDisposable.Dispose Dispose(True) GC.SuppressFinalize(True) End Sub Protected Sub Dispose(ByVal disposing As Boolean) If disposing <> True Then Return End If

If SqlConn Is Nothing = False Then SqlConn.Dispose() SqlConn = Nothing End If End Sub

Public Sub Open() If SqlConn Is Nothing = True Then '建立数据库连接对象 SqlConn = New SqlConnection(Me.sConn) '打开数据库连接 SqlConn.Open() End If End Sub Public Sub Close() '如果数据库连接对象不为空则关闭数据库连接 If SqlConn Is Nothing = False Then SqlConn.Close() End If End Sub Public Function RunSelectSQL(ByVal sSQLString As System.String) As DataView Me.Open() Dim SqlDS As DataSet = New DataSet Dim SqlDA As SqlDataAdapter = New SqlDataAdapter(sSQLString, Me.SqlConn) SqlDA.Fill(SqlDS) Return SqlDS.Tables(0).DefaultView End Function Public Function RunDelOrInsSQL(ByVal sSQLString _ As System.String) Me.Open() Dim SqlComm As SqlCommand = _ New SqlCommand(sSQLString, Me.SqlConn) SqlComm.ExecuteNonQuery() End Function End Class 再做界面: Imports System.Data Imports System.Data.SqlClient

Public Class frmLogin Inherits System.Windows.Forms.Form

Public bresult As Boolean = False Public usernumber As String = "" Public username As String = "" Private userpass As String = "" #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 Panel1 As System.Windows.Forms.Panel Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents txtpass As System.Windows.Forms.TextBox Friend WithEvents txtname As System.Windows.Forms.TextBox Friend WithEvents txtnumber As System.Windows.Forms.TextBox Friend WithEvents btnsubmit As System.Windows.Forms.Button Friend WithEvents btnclear As System.Windows.Forms.Button Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Label5 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmLogin)) Me.Label1 = New System.Windows.Forms.Label Me.Panel1 = New System.Windows.Forms.Panel Me.btnclear = New System.Windows.Forms.Button Me.btnsubmit = New System.Windows.Forms.Button Me.Label5 = New System.Windows.Forms.Label Me.txtpass = New System.Windows.Forms.TextBox Me.txtname = New System.Windows.Forms.TextBox Me.txtnumber = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.Panel1.SuspendLayout() Me.SuspendLayout() ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("隶书", 36.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte)) Me.Label1.ForeColor = System.Drawing.SystemColors.ActiveCaption Me.Label1.Location = New System.Drawing.Point(272, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(64, 240) Me.Label1.TabIndex = 2 Me.Label1.Text = "海普制盖" ' 'Panel1 ' Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Panel1.Controls.Add(Me.btnclear) Me.Panel1.Controls.Add(Me.btnsubmit) Me.Panel1.Controls.Add(Me.Label5) Me.Panel1.Controls.Add(Me.txtpass) Me.Panel1.Controls.Add(Me.txtname) Me.Panel1.Controls.Add(Me.txtnumber) Me.Panel1.Controls.Add(Me.Label4) Me.Panel1.Controls.Add(Me.Label3) Me.Panel1.Controls.Add(Me.Label2) Me.Panel1.Location = New System.Drawing.Point(336, 0) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(272, 272) Me.Panel1.TabIndex = 3 ' 'btnclear ' Me.btnclear.Location = New System.Drawing.Point(176, 240) Me.btnclear.Name = "btnclear" Me.btnclear.Size = New System.Drawing.Size(40, 16) Me.btnclear.TabIndex = 8 Me.btnclear.Text = "Button2" Me.btnclear.Visible = False ' 'btnsubmit ' Me.btnsubmit.Location = New System.Drawing.Point(56, 240) Me.btnsubmit.Name = "btnsubmit" Me.btnsubmit.Size = New System.Drawing.Size(48, 16) Me.btnsubmit.TabIndex = 7 Me.btnsubmit.Text = "Button1" Me.btnsubmit.Visible = False ' 'Label5 ' Me.Label5.Font = New System.Drawing.Font("隶书", 24.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte)) Me.Label5.Location = New System.Drawing.Point(8, 16) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(152, 48) Me.Label5.TabIndex = 6 Me.Label5.Text = "请登陆:" Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'txtpass ' Me.txtpass.Location = New System.Drawing.Point(96, 208) Me.txtpass.Name = "txtpass" Me.txtpass.PasswordChar = Microsoft.VisualBasic.ChrW(42) Me.txtpass.Size = New System.Drawing.Size(144, 21) Me.txtpass.TabIndex = 5 Me.txtpass.Text = "" ' 'txtname ' Me.txtname.Location = New System.Drawing.Point(96, 144) Me.txtname.Name = "txtname" Me.txtname.ReadOnly = True Me.txtname.Size = New System.Drawing.Size(144, 21) Me.txtname.TabIndex = 4 Me.txtname.Text = "" ' 'txtnumber ' Me.txtnumber.Location = New System.Drawing.Point(96, 80) Me.txtnumber.Name = "txtnumber" Me.txtnumber.Size = New System.Drawing.Size(144, 21) Me.txtnumber.TabIndex = 3 Me.txtnumber.Text = "" ' 'Label4 ' Me.Label4.Font = New System.Drawing.Font("宋体", 15.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte)) Me.Label4.Location = New System.Drawing.Point(0, 208) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(88, 24) Me.Label4.TabIndex = 2 Me.Label4.Text = "密 码:" Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Label3 ' Me.Label3.Font = New System.Drawing.Font("宋体", 15.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte)) Me.Label3.Location = New System.Drawing.Point(0, 144) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(88, 24) Me.Label3.TabIndex = 1 Me.Label3.Text = "姓 名:" Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("宋体", 15.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte)) Me.Label2.Location = New System.Drawing.Point(0, 80) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(88, 24) Me.Label2.TabIndex = 0 Me.Label2.Text = "工 号:" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'PictureBox1 ' Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image) Me.PictureBox1.Location = New System.Drawing.Point(0, 0) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(272, 272) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False ' 'frmLogin ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(608, 254) Me.Controls.Add(Me.Panel1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.PictureBox1) Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon) Me.MaximizeBox = False Me.Name = "frmLogin" Me.Text = "登陆、、、" Me.Panel1.ResumeLayout(False) Me.ResumeLayout(False)

End Sub

#End Region

Private Sub txtnumber_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtnumber.LostFocus '定义一个数据访问类的对象 Dim db As database = New database '根据输入的用户ID进行查询 Dim dv As DataView = _ db.RunSelectSQL _ ("select 密码,姓名 from ygda where 工号 = '" _ + Me.txtnumber.Text.Trim() + "'") '判断用户ID是否存在 If dv.Count = 0 Then '用户ID不存在 Me.txtnumber.Text = "" Me.txtpass.Text = "" Me.txtname.Text = "" Else '用户ID存在 username = Me.txtnumber.Text.Trim() '获得用户的密码级用户名 Me.userpass = dv.Item(0)("密码") Me.txtname.Text = dv.Item(0)("姓名") End If '释放数据访问类的占用资源 db.Dispose() End Sub

Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click '判断用户输入密码的合法性 If Me.userpass.Trim() = Me.txtpass.Text.Trim() _ And userpass <> "" Then '合法用户 bresult = True username = Me.txtname.Text.Trim() Me.Close() Else '密码错误 bresult = False MessageBox.Show("请确认你的用户名或密码是否正确") End If End Sub

Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click End

End Sub

Private Sub txtnumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnumber.KeyPress If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then Me.txtpass.Focus() End If End Sub

Private Sub txtpass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpass.KeyPress If e.KeyChar = Microsoft.VisualBasic.ChrW(13) _ And userpass <> "" Then '是回车键并调用【确定】按钮的单击事件响应函数 Me.btnsubmit_Click(Nothing, Nothing) End If End Sub

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

End Sub End Class


2005-07-09 16:32
雪中浪人
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2005-7-8
收藏
得分:0 
高手!!!!小弟谢谢指点,但是能否将该程序发到我的邮箱:hchx1@

2005-07-09 17:16
chj18888
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-6-22
收藏
得分:0 
我再续一点:
如果是server数据库想把连接用户名和服务器名等储存到ini文件里首次运行连接数据库!并保存数据,以后读取ini文件连接,出错了返回从新连接这个中间的初始加载怎么做?
2005-07-26 16:44
快速回复:~~~如何在VB中实现与数据库相联系的登陆窗口~~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023958 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved