c#窗体应用程序----登录(简单的字符串比较/Mysql数据库)

很多时候我们程序操作都得先登录, 所以为了完善我今天就向着给我的窗体程序加了一个登录过程. 废话不多说, 写起来.

分两部分, 一是简单的对比字符串, 进一步连接数据库(最好这么做).

一. 字符串比较登录(简单)

首先工程建立, 新建窗体就略过了

两个窗体文件: WinLogin.cs Form1.cs

一个主程序入口: Program.cs

(1)登录窗体 :

界面如下:

登录成功页面:

进入主页面:

觉得这部分很简单直接上代码:

public WinLogin()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            //1. 获取数据
            //从TextBox中获取用户输入信息
            string userName = this.txtUserName.Text;
            string userPassword = this.txtPassword.Text;

            //2. 验证数据
            // 验证用户输入是否为空,若为空,提示用户信息
            if (userName.Equals("") || userPassword.Equals(""))
            {
                MessageBox.Show("用户名或密码不能为空!");
            }
            // 若不为空,验证用户名和密码是否与数据库匹配
            // 这里只做字符串对比验证
            else
            {
                //用户名和密码验证正确,提示成功,并执行跳转界面。
                if (userName.Equals("admin") && userPassword.Equals("admin"))
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                    MessageBox.Show("登录成功!");
                    //Application.Run(new Form1());
                    /**
                     * 待添加代码区域
                     * 实现界面跳转功能
                     * 
                     */


                }
                //用户名和密码验证错误,提示错误。
                else
                {
                    MessageBox.Show("用户名或密码错误!");

                }
            }

            //3. 处理数据
        }
        //退出按钮
        private void button1_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
        //忘记密码
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore.exe", "https://218.4.33.72:8093/#/login");
        }
    }

(2)主窗体 : 这一部分目前只需要新建一个winform窗体就行了, 具体开发与本文无关

我们的主程序入口程序:

static class Program

    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            WinLogin winLogin = new WinLogin();
            if (winLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Form1());
            }
        }
    }

二. 基于MySql登录(也不难)

这一部分就是把用户输入的账号和密码与数据库的账号密码对比, 匹配成功就登录成功, 否则就登录失败.

建立连接:

class LocationDA  
        {  
            public List<LocationData> select()  
            {  
                List<LocationData> list = null;  
                string ConString = "server=localhost;User Id=root;password=root;Database=restful_api;SslMode = none;";  
                MySqlConnection conn = new MySqlConnection(ConString);//连接数据库    
                try  
                {  
                    conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句  
                    Console.WriteLine("已经建立连接");  
                    //在这里使用代码对数据库进行增删查改  
                }  
                catch (MySqlException ex)  
                {  
  
                    Console.WriteLine("建立连接失败!");  
                    Console.WriteLine(ex.Message);  
                }  
                //SqlCommand cmd = conn.CreateCommand();  
                MySqlCommand cmd = conn.CreateCommand();  
                String admin=admin.getText();
                cmd.CommandText = "select * from location where name=admin";//从表里选择数据
                MySqlDataReader dr = cmd.ExecuteReader();  
                if (dr.HasRows)  
                {  
                    list = new List<CarData>();  
                    while (dr.Read())  
                    {  
                        LocationData data = new LocationData();  
                        data = new LocationData();  
                        data.Value = (int)dr["value"];  
                        list.Add(data);  
                    }  
                }  
                return list;  
                cmd.Dispose();  
                conn.Close();  
            }  
        }

数据库查询语句

String admin=admin.getText();
cmd.CommandText = "select * from location where name=admin";//从表里选择数据
根据账户查询那一条记录, 然后再匹配密码是否正确

猜你喜欢

转载自blog.csdn.net/qq_37832932/article/details/80961061