如何用c#编写一个登录界面(内含access2010数据库)

本人新手菜鸟,最近因为学习需要,编写了一个登录界面。这个对于一些大牛来说是非常简单的,但是对于新手来说,还是有点参考意见。本文参考了一些文章,但是看的太多记不大清楚出处,在此表示歉意。本文若有什么不对的地方或者需要改进的地方,还请各位帮忙指正,小弟在此谢过。。。废话不多说,开始正题。在这里插入图片描述
登录界面如上图所示,第一个combobox的Name是cb_useName,第二个text的Name是T_password.此程序包含的access数据库放在该程序的debug文件夹中,因为对连接数据库的字符串的方法还没去领会,所以暂时采用这种比较简单的连接方式。。。。代码如下

   private void button1_Click(object sender, EventArgs e)
    {
        if(cb_useName.Text.Length==0)
        
        {
            MessageBox.Show("用户名不能为空...");
        }
        else if(T_passWord.Text.Length==0)
        {
            MessageBox.Show("密码不能为空...");
        }
        else
        {
            string userName = cb_useName.Text.Trim();
            string passWord = T_passWord.Text.Trim();
            string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Liangcang.accdb";
            OleDbConnection con = new OleDbConnection(conStr);
            con.Open();
            string cmdStr = "select * from Users where userName='" + this.cb_useName.Text + "'and Password='" + this.T_passWord.Text + "'";
            OleDbCommand cmd = new OleDbCommand(cmdStr, con);
            OleDbDataReader reader = cmd.ExecuteReader();
            
            if (reader.Read()) //如果找到用户信息,说明登录成功
            {

                MessageBox.Show("登录成功!");
                MainForm newForm = new MainForm();
                newForm.Show(); //弹出主窗体
                this.Hide(); //隐藏用户登录窗体
            }
            else
            {
                MessageBox.Show("用户名或密码错误!");
            }
            reader.Close();
            //cmd.Connection.Close();
            con.Close();

        }
       
    }`
    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
        this.Dispose();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            //密码显示框勾选,显示明文
            T_passWord.PasswordChar = new char();
        }
        else
        {
            //密码显示框未勾选,显示密文
            T_passWord.PasswordChar = '*';
        }
    }
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是实现步骤: 1. 创建一个 C# 窗体应用程序,命名为 LoginDemo。 2. 在 LoginDemo 窗体中设计一个登录页面,包括用户名和密码的输入框、登录按钮和注册按钮。 3. 设计一个注册页面,包括用户名、密码、确认密码和邮箱的输入框以及注册按钮。 4. 在数据库中创建一个名为 LoginDemo 的数据库,在其中创建一个名为 Users 的表,包括 id、username 和 password 三个字段。 5. 在 Visual Studio 中打开 Server Explorer,连接到本地的 SQL Server 数据库,找到 LoginDemo 数据库,在其中创建 Users 表。 6. 在 LoginDemo 窗体中,添加连接数据库的代码。在窗体中右键单击,选择“添加->新建项->数据->数据集”,在数据集设计器中添加一个新的数据集,命名为 LoginDemoDataSet。 7. 在数据集设计器中添加一个新的数据表适配器,命名为 UsersTableAdapter,设置它的 SQL 语句为“SELECT id, username, password FROM Users”。 8. 在 LoginDemo 窗体中,添加代码连接数据库并获取用户输入的用户名和密码,判断用户名和密码是否正确,并根据结果跳转到主窗体或者弹出错误提示框。 9. 在注册页面中,添加代码将用户输入的用户名和密码插入到数据库中,并跳转到登录页面。 示例代码如下: ``` // 连接数据库 SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=LoginDemo;Integrated Security=True"); conn.Open(); // 获取用户输入的用户名和密码 string username = txtUsername.Text; string password = txtPassword.Text; // 查询数据库中是否存在该用户 SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE username=@username AND password=@password", conn); cmd.Parameters.AddWithValue("@username", username); cmd.Parameters.AddWithValue("@password", password); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { // 用户名和密码正确,跳转到主窗体 Mainform mainform = new Mainform(); mainform.Show(); this.Hide(); } else { // 用户名或密码错误,弹出错误提示框 MessageBox.Show("用户名或密码错误!"); } // 关闭数据库连接 conn.Close(); ``` 在注册页面中,添加插入数据的代码: ``` // 连接数据库 SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=LoginDemo;Integrated Security=True"); conn.Open(); // 获取用户输入的用户名、密码和邮箱 string username = txtUsername.Text; string password = txtPassword.Text; string email = txtEmail.Text; // 插入数据到数据库中 SqlCommand cmd = new SqlCommand("INSERT INTO Users(username, password, email) VALUES(@username, @password, @email)", conn); cmd.Parameters.AddWithValue("@username", username); cmd.Parameters.AddWithValue("@password", password); cmd.Parameters.AddWithValue("@email", email); cmd.ExecuteNonQuery(); // 关闭数据库连接 conn.Close(); // 跳转到登录页面 Loginform loginform = new Loginform(); loginform.Show(); this.Hide(); ``` 注意:以上示例代码仅供参考,具体实现根据你的需求进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值