標籤:rand cmd task btn 介面 ted 對象 ini pre
//編寫登入介面邏輯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(); } }
C#_串連資料庫實現 登入註冊介面