C#實現登錄功能(連接SQLServer數據庫)


本例使用C#實現一個簡單的登錄功能,分為用戶和管理員兩個角色登錄。

效果圖:

 

 

 

 

 

 

 

 

 

 

 

 

 

 核心代碼

login.cs

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                loginCheck();
            }
            else
            {
                MessageBox.Show("賬號和密碼不能為空");
            }
        }
        //登錄驗證
        public void loginCheck()
        {
            if (userName.Checked == true)
            {
                Dao dao = new Dao();//實例化數據庫對象
                string sql="select * from t_user where id='"+textBox1.Text+"' and pwd='"+textBox2.Text+"'";
                IDataReader dc = dao.read(sql);
                if (dc.Read())
                {
                    Data.UID = dc["id"].ToString();
                    Data.UName = dc["name"].ToString();

                    MessageBox.Show("用戶角色登錄成功");
                    user1 user = new user1();
                    this.Hide();
                    user.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("密碼錯誤,登錄失敗");
                }
                dao.DaoClose();
            }

            if (adminName.Checked == true)
            {
                Dao dao = new Dao();//實例化數據庫對象
                string sql = "select * from t_admin where id='" + textBox1.Text + "' and pwd='" + textBox2.Text + "'";
                IDataReader dc = dao.read(sql);
                if (dc.Read())
                {
                    MessageBox.Show("管理員角色登錄成功");
                    admin1 admin = new admin1();
                    this.Hide();
                    admin.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("密碼錯誤,登錄失敗");
                }
                dao.DaoClose();
            }
        }

Data.cs

class Data
    {
        public static string UID = "", UName = "";//登錄用戶的id和姓名
    }

Dao.cs

class Dao
    {
        SqlConnection sc;
        public SqlConnection connect()
        {
            string str = @"Data Source=LAPTOP-H45ALQK7;Initial Catalog=BookDB;Integrated Security=True";//配置數據庫
            sc = new SqlConnection(str);//創建數據庫連接對象
            sc.Open();//打開數據庫
            return sc;//返回數據庫連接對象
        }
        public SqlCommand command(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connect());
            return cmd;
        }
        public int Execute(string sql)//更新操作
        {
            return command(sql).ExecuteNonQuery();
        }
        public SqlDataReader read(string sql)//讀取操作
        {
            return command(sql).ExecuteReader();
        }
        public void DaoClose()
        {
            sc.Close();//關閉數據庫
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM