【第二次實驗進度博客】C#登錄界面可視化

本次使用數據庫來和登陸界面的窗口連接起來。
首先在數據庫中創建賬戶管理庫:

create table login1(account char(13),
    pass char(13)
    )
insert into login1(account,pass) values('qqabc','123')
insert into login1(account,pass) values('12345','qq123')


 

 

 

 

最終效果:

登陸成功:

本想仿照着連接數據庫獲取賬戶密碼的方法做一個註冊的頁面,將下面的text1和text2裏的數據插入到數據庫login1中,不過始終都有錯誤:

查了一些資料,感覺可能是數據庫裏面的定義的數據類型是char,而在註冊頁面裏的text框裏獲取的字符類型爲string,本想把數據庫裏的數據類型也改成string卻發現數據庫裏的數據類型沒有string。這部分有點超出我的認知範圍了,註冊還是等後面學完如何插入數據後再來繼續修改吧。

Form1代碼:

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 LoginDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Sign sign = new Sign();
            sign.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string username = textBoxUserName.Text.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=admin";



            SqlConnection sqlConnection = new SqlConnection(myConnString);  //實例化連接對象

            sqlConnection.Open();



            string sql = "select account,pass from login1 where account = '" + username + "' and pass = '" + password + "'";                                            //編寫SQL命令

            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);



            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();



            if (sqlDataReader.HasRows)

            {

                MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);             //登錄成功

                //  label1.Text = "Log in :" + username;

                //Form2 form2 = new Form2();

                //form2.Show();

                this.Hide();
                FormMain formMain = new FormMain();
                formMain.Show();

            }

            else

            {

                MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            sqlConnection.Close();
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

FormMain代碼:

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 LoginDemo
{
    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: 這行代碼將數據加載到表“tESTDataSet.Student”中。您可以根據需要移動或刪除它。
            this.studentTableAdapter.Fill(this.tESTDataSet.Student);

        }
    }
}

註冊部分代碼:

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 LoginDemo
{
    public partial class Sign : Form
    {
        public Sign()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text.Trim();
            string s2 = textBox2.Text.Trim();
            string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=admin";
            SqlConnection sqlConnection = new SqlConnection(myConnString);  //實例化連接對象
            sqlConnection.Open();
            string sql = "insert into login2(account,pass) values(s1,s2)";                                            //編寫SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.HasRows)
            {
                MessageBox.Show("註冊成功!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);             //登錄成功
                //  label1.Text = "Log in :" + username;
                //Form2 form2 = new Form2();
                //form2.Show();
                this.Hide();
                Form1 form1 = new Form1();
                form1.Show();
            }
            else
            {
                MessageBox.Show("註冊失敗!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            sqlConnection.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

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


 

 

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