做一个简单的用户登录界面,初步了解C#,后续还会加入增删改查用户的功能,敬请期待。

这个是form1的效果图和代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;//用access数据库连接时需添加这句

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1.创建路径及数据库名
            //string strPath = Application.StartupPath + "\\dengLu.accdb";
            //2.生成链接数据库字符串           
            //string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Pc\\Desktop\\WindowsFormsApplication1\\WindowsFormsApplication1\\bin\\Debug\\dengLu.accdb";//我把Access数据库放入bin\\debug中(你可以放入任何文件夹,只要把路径写对就可以),这种方式是绝对路径
            string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\dengLu.accdb";//把Access数据库放入bin\\debug中,这种方式是相对路径(数据库必须放在的debug中)
            //3.创建数据库连接
            OleDbConnection conn = new OleDbConnection(conStr);
            //4.当创建好连接到Access后打开数据库连接
            conn.Open();
            //5.SQL查询语句
            string Access = "select * from yonghu where UserName='" + this.textBox1.Text + "'";//select是查询数据库语句
            string Access1 = "select * from yonghu where UserName='" + this.textBox1.Text + "'and PassWord='" + this.textBox2.Text + "'";//select是查询数据库语句
            //6.新声明一个OleDbCommand对象
            OleDbCommand cmd = new OleDbCommand(Access, conn);                        
            //7.从数据库读出来的数据流赋给read,简而言之就是读取数据库中实时数据流
            OleDbDataReader read = cmd.ExecuteReader();           
            if (read.Read() == true) {
                OleDbCommand cmd1 = new OleDbCommand(Access1, conn);
                OleDbDataReader read1 = cmd1.ExecuteReader();
                if (read1.Read() == true)
                {                    
                    Form2 newForm2 = new Form2();
                    newForm2.Show();//到Form2
                        Hide();//隐藏此窗口
                }
                else
                {
                    MessageBox.Show("密码错误!");
                }
            }
            else
            {
                    MessageBox.Show("账户错误!");
            }            
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();//关闭整个程序
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

当点击退出时,直接退出程序:

这个是form2的效果图和代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
   public partial class Form2 : Form
   {       
       public Form2()
       {
           InitializeComponent();
           label2.Text = "登陆成功!";
       }
       private void button1_Click(object sender, EventArgs e)
       {
           Form1 newForm1 = new Form1();
           newForm1.Show();//到Form1
           Hide();//隐藏此窗口
       }
       private void textBox1_TextChanged(object sender, EventArgs e)
       {            
       }
       private void label1_Click(object sender, EventArgs e)
       {
       }
       private void label2_Click(object sender, EventArgs e)
       {         
       }
       private void label3_Click(object sender, EventArgs e)
       {
       }
       private void label4_Click(object sender, EventArgs e)
       {
       }
       private void button2_Click(object sender, EventArgs e)
       {
       }
       private void button3_Click(object sender, EventArgs e)
       {
       }
       private void Form2_Load(object sender, EventArgs e)
       {
       }
   }
}

下面是做出的效果图:

(1)输入账户和密码点击登录:

(2)登陆成功:

(3)点击返回按钮可以返回到主页面:

(4)错误时进行分析:
①如果是账号错误:

②如果是密码错误:

初步完成用户登录界面,后续还会继续完善增删改查功能。

更多推荐

C#初学者做一个简单的用户登陆界面用Access数据库