在原先登录程序上更改数据存储到数据库中
登录窗口代码
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Login
{
public partial class SignInForm : Form
{
public SignInForm()
{
InitializeComponent();
}
private void 登录_Load(object sender, EventArgs e) { }
private void textBox1_TextChanged(object sender, EventArgs e) { }
private void Password_TextChanged(object sender, EventArgs e)
{
if (!checkBox1.Checked)
{
Password.PasswordChar = '*';
}
}
private void button3_Click(object sender, EventArgs e)
{
Form f1 = new SignUpForm();
f1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
int Flag = 0;
String strUser, strPassword;
sqlConnection1.Open();
SqlDataReader dataReader = sqlCommand1.ExecuteReader();
while(dataReader.Read())
{
Flag = 1;
strUser = dataReader["用户名"].ToString();
strPassword = dataReader["密码"].ToString();
if (strUser == this.UseName.Text && strPassword == this.Password.Text)
{
MessageBox.Show("Hello world!");
Flag = 2;
break;
}
}
if (Flag == 0)
MessageBox.Show("当前无用户,请前往注册!");
if(dataReader.Read()==false && Flag==1)
{
MessageBox.Show("用户名或密码错误!");
this.UseName.Clear();
this.Password.Clear();
checkBox1.Checked = false;
this.UseName.Focus();
}
dataReader.Close();
sqlConnection1.Close();
}
private void button2_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
Password.PasswordChar = new char(); //显示密码
else
{
Password.PasswordChar = '*';
}
}
}
}
注册窗口代码
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Login
{
public partial class SignUpForm : Form
{
public SignUpForm()
{
InitializeComponent();
}
public void SignUp_Load(object sender, EventArgs e){}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.PasswordChar = '*';
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
textBox4.PasswordChar = '*';
}
public void button1_Click(object sender, EventArgs e)
{
if (textBox3.Text == textBox4.Text)
{
String bent = "", sex = "";
if (radioButton1.Checked)
{
sex = "男";
}
else if (radioButton2.Checked)
{
sex = "女";
}
foreach (Control c in groupBox2.Controls)
{ //获取爱好所选选项
if (c is CheckBox && ((CheckBox)c).Checked == true)
{
bent += c.Text + " ";
}
}
sqlConnection1.Open();
string sqlstr = "insert into 数据表 VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + dateTimePicker1.Value + "','" + sex + "','" + bent + "')";
SqlCommand sqlCommand = new SqlCommand(sqlstr, sqlConnection1);
sqlCommand.ExecuteNonQuery();
sqlConnection1.Close();
MessageBox.Show("注册成功,请返回登录。");
this.Close();
}
else
{
MessageBox.Show("两次密码不同!");
this.textBox4.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{ //点击取消清除数据
this.textBox1.Clear();
this.textBox2.Clear();
this.textBox3.Clear();
this.textBox4.Clear();
this.textBox1.Focus();
this.radioButton1.Checked = false;
this.radioButton2.Checked = false;
this.checkBox3.Checked = false;
this.checkBox4.Checked = false;
this.checkBox5.Checked = false;
this.checkBox6.Checked = false;
this.checkBox7.Checked = false;
}
}