實驗反饋2—連接數據庫、新建窗體、密碼加密、數據窗口DataGridView

本週學習新的內容,如何連接數據庫,將密碼加密、新建下一個窗體以及數據窗口DataGridView的部分內容。
數據庫中的參考內容:
在這裏插入圖片描述
在這裏插入圖片描述

1、代碼:
連接數據庫部分代碼:代碼中附帶部分語句的解釋

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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void labellogin_Click(object sender, EventArgs e)
        {

        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            string username = textBoxusername.Text.Trim();          //取出賬號,Trim()用於去掉前後的空格
            string password = textBoxpassword.Text.Trim();         //取出密碼

            //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //讀取連接字符串
            string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123";      //連接數據庫
                  //服務器名字:本機或其他IP地址;   數據庫名稱;                                   
           
            SqlConnection sqlconnection = new SqlConnection(myConnString);      //實例化連接對象
            sqlconnection.Open();

            string sql = "select userid,password from login where userid='" + username + "'and password='" + password + "'";            //編寫SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlconnection);             //建立數據庫於C#的聯繫

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();               //讀取數據

            if (sqlDataReader.HasRows)
            {
                MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);       //登錄成功
                labellogin.Text = "Log in:" + username;
                
                //進入下一個窗體,將之前的窗體隱藏
                Formmain formmain = new Formmain();
                formmain.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);           //登錄失敗
            }
            sqlconnection.Close();
        }

        private void buttoncancel_Click(object sender, EventArgs e)
        {
            Application.Exit();     //退出整個應用程序
        }
    }
}

下一個窗體,點擊取消按鈕完成操作

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;

namespace LOGIN
{
    public partial class Formmain : Form
    {
        public Formmain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Formmain_Load(object sender, EventArgs e)
        {
            // TODO: 這行代碼將數據加載到表“studentDataSet.Student”中。您可以根據需要移動或刪除它。
            this.studentTableAdapter.Fill(this.studentDataSet.Student);

        }
    }
}

2、新建下一個窗體。
點擊【解決方案資源管理器】,點擊右鍵,【添加】-【窗體】。
選擇【窗體】,可以在名稱處修改名稱,點擊【添加】。即可產生新的窗體。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
3、關於如何將密碼加密,不顯示具體的信息:
點擊密碼的文本框,右鍵點擊【屬性】,在屬性中找出【PasswordChar】,將其修改爲*或者.,就可以將密碼加密。
在這裏插入圖片描述
4、數據窗口-DataGridView
在這裏插入圖片描述
將數據窗口拖拽到新建的窗體中,在窗口的右上角點擊三角形,在【選擇數據源】中選擇【添加項目數據源】,選擇【數據庫】,點擊【下一步】,選擇【數據集】,【下一步】,【新建連接】,【服務器名】爲本機.,修改【身份驗證爲SQL Server驗證】,填寫【用戶名】和【密碼】,在【選擇或輸入相應的數據庫名稱】處選擇需要的數據庫,這裏選擇Student數據庫。點擊【確定】後,在【選擇數據庫對象】中選擇相應的表,【完成】即可建立數據窗口,運行時將Student表中的信息顯示在窗體中。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

5、運行測試:
正確登陸時:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
錯誤登錄時:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章