//编写登录界面逻辑usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceLoginDatabase
{public partial classLogin : Form
{private int errorTime = 3;publicLogin() {
InitializeComponent();
}private void loginBtn_Click(objectsender, 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(objectsender, EventArgs e) {
Application.Exit();
}private void button1_Click(objectsender, EventArgs e) {
Register register= newRegister();
register.ShowDialog();
}private void checkCode_Click(objectsender, EventArgs e) {
Random random= newRandom();int minV = 12345, maxV = 98765;
checkCode.Text=random.Next(minV, maxV).ToString();
}
}
}5.编写注册界面逻辑usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceLoginDatabase
{public partial classRegister : Form
{publicRegister() {
InitializeComponent();
}private void btnRegister_Click(objectsender, 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(objectsender, EventArgs e) {
Random random= newRandom();int minV = 12345, maxV = 98765;
checkCode.Text=random.Next(minV, maxV).ToString();
}
}