使用数据库创建登录表单 [英] Create a login form with database

查看:60
本文介绍了使用数据库创建登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个将用户凭据存储在数据库中的登录表单.我想用C#编程.
谁能帮助我入门?

How can I create a login form where the user credentials are stored in a database. I want to program this in C#.
Can anyone help me get started?

Thanks for your time!

推荐答案

这里有一些我使用的代码,对我来说很完美!
我正在Windows 7 Ultimate x64上使用Microsoft Access 2007和Visual Studio 2010 Ultimate.

您需要将连接字符串和路径更改为实际数据库文件

代码:
将此添加到您的使用"部分:
Here''s some code I used, works perfect for me!
I''m using Microsoft Access 2007 and Visual Studio 2010 Ultimate on Windows 7 Ultimate x64.

You''ll need to change the connection string and path to the actual database file

Code:
Add this in your Using Section:
using System.Data.OleDb;

string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\MedDesk\\Login.accdb;";
            OleDbConnection MyConn = new OleDbConnection(ConnStr);
            try
            {
                MyConn.Open();
                string StrCmd = "SELECT User, Pass FROM LoginTable WHERE User=''"+user.Text + "'' and Pass=''" + pass.Password + "''";
                OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn); ;
                OleDbDataReader ObjReader = Cmd.ExecuteReader();
                if (ObjReader != null)
                {
                    new MainWindow().Show();
                }
                ObjReader.Close();
                MyConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


1)将新表单添加到您的项目中.
2)添加两个标签,两个文本框和两个按钮.
3)将标签文本更改为用户名"和密码"
4)将文本框名称更改为"tbUsername"和"tbPassword"
5)在"tbPassword"文本框的属性窗格中,将"UseSystemPasswordChar"设置为true
6)将两个按钮名称分别更改为"butOK"和"butCancel"
7)在属性窗格中,将butOK.DialogResult设置为确定",并将butCancel.DialogResult设置为取消"
8)双击确定"按钮.

以通常的方式在您的代码中显示表单.如果DialogResult确定,请针对您的数据库选中两个文本框.如果它们不相同,请再次显示该表格.
1) Add a new form to your project.
2) Add two labels, two textboxes, and two buttons.
3) Change the label texts to "Username" and "Password"
4) Change the textbox names to "tbUsername" and "tbPassword"
5) In the properties pane for the "tbPassword" textbox, set "UseSystemPasswordChar" to true
6) Change the two button names to "butOK" and "butCancel"
7) In the properties pane, set butOK.DialogResult to "OK" and butCancel.DialogResult to "Cancel"
8) Double click the OK button.

Show the form in your code in the usual way. If the DialogResult is OK, check the two text boxes against your database. If they are not the same, show the form again.


这篇关于使用数据库创建登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆