1.主界面代碼:
2.注冊頁面
3.登陸界面
登陸注冊代碼:

//編寫登錄界面邏輯 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.SqlClient; namespace LoginDatabase { public partial class Login : Form { private int errorTime = 3; public Login() { InitializeComponent(); } private void loginBtn_Click(object sender, EventArgs e) { errorTime = errorTime - 1; string username = txtName.Text.Trim(); //取出賬號 string pw = txtPwd.Text.Trim(); //取出密碼 string constr = "Server=.;DataBase=SU; Integrated Security=True"; //設置連接字符串 SqlConnection mycon = new SqlConnection(constr); //實例化連接對象 mycon.Open(); SqlCommand mycom = mycon.CreateCommand(); //創建SQL命令執行對象 string s1 = "select account,password from register where account='" + username + "' and password='" + pw + "'"; //編寫SQL命令 mycom.CommandText = s1; //執行SQL命令 SqlDataAdapter myDA = new SqlDataAdapter(); //實例化數據適配器 myDA.SelectCommand = mycom; //讓適配器執行SELECT命令 DataSet myDS = new DataSet(); //實例化結果數據集 int n = myDA.Fill(myDS, "register"); //將結果放入數據適配器,返回元祖個數 if (n != 0) { if (checkCode.Text == textCheck.Text) { MessageBox.Show("歡迎使用!"); //登錄成功 this.Close(); } else { MessageBox.Show("驗證碼填寫錯誤"); textCheck.Text = ""; } } else if (errorTime < 3) { MessageBox.Show("用戶名或密碼有錯。請重新輸入!還有" + errorTime.ToString() + "次機會"); txtName.Text = ""; //清空賬號 txtPwd.Text = ""; //清空密碼? txtName.Focus(); //光標設置在賬號上 } else { MessageBox.Show("你輸入的用戶名或密碼已達三次? 將退出程序"); this.Close(); } } private void cancelBtn_Click(object sender, EventArgs e) { Application.Exit(); } private void button1_Click(object sender, EventArgs e) { Register register = new Register(); register.ShowDialog(); } private void checkCode_Click(object sender, EventArgs e) { Random random = new Random(); int minV = 12345, maxV = 98765; checkCode.Text = random.Next(minV, maxV).ToString(); } } } 5.編寫注冊界面邏輯 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; using System.Threading.Tasks; using System.Windows.Forms; namespace LoginDatabase { public partial class Register : Form { public Register() { InitializeComponent(); } private void btnRegister_Click(object sender, EventArgs e) { //檢查是否已經存在 string userID = userId.Text.Trim(); //取出賬號 /** * 連接數據庫 */ string constr = "Server=.;DataBase=SU; Integrated Security=True"; //設置連接字符串 SqlConnection mycon = new SqlConnection(constr); //實例化連接對象 mycon.Open(); //查詢新注冊的用戶是否存在 SqlCommand checkCmd = mycon.CreateCommand(); //創建SQL命令執行對象 string s = "select account from register where account='" + userID + "'"; checkCmd.CommandText = s; SqlDataAdapter check = new SqlDataAdapter(); //實例化數據適配器 check.SelectCommand = checkCmd; //讓適配器執行SELECT命令 DataSet checkData = new DataSet(); //實例化結果數據集 int n = check.Fill(checkData, "register"); //將結果放入數據適配器,返回元祖個數 if (n != 0) { MessageBox.Show("用戶名存在"); userId.Text = ""; userPw.Text = ""; nickName.Text = ""; } //確認密碼 if (ensurePw.Text != userPw.Text) { ensurePw.Text = ""; } //驗證碼 if (textCheck.Text != checkCode.Text) { textCheck.Text = ""; } //插入數據SQL 邏輯 string s1 = "insert into Register(account,password,nickname) values ('" + userId.Text + "','" + userPw.Text + "','" + nickName.Text + "')"; //編寫SQL命令 SqlCommand mycom = new SqlCommand(s1, mycon); //初始化命令 mycom.ExecuteNonQuery(); //執行語句 mycon.Close(); //關閉連接 mycom = null; mycon.Dispose(); //釋放對象 if (userId.Text == "" || userPw.TextLength <= 6 || nickName.Text == "" || ensurePw.Text == "" || textCheck.Text == "") { MessageBox.Show("請將信息填完整"); } else { MessageBox.Show("注冊成功"); this.Close(); } } private void checkCode_Click(object sender, EventArgs e) { Random random = new Random(); int minV = 12345, maxV = 98765; checkCode.Text = random.Next(minV, maxV).ToString(); } }