登陆窗体做成像qq那样的能记住密码的功能,现在数据库里面的管理员密码采用的是md5加密算法,管理员表有四个字段:id,name ,password,status,其中status是记住checkbox复选框是否被选中('真'或'假')。现在的问题是:如果数据库的密码字段不用md5加密,能够实现,但是如果加密就读不出来了,请大家帮帮忙啊!以下是关键代码:
private void LoginForm_Load(object sender, EventArgs e)
{
string path = "login.bin";
if (!File.Exists(path))
{
return;
}
FileStream fs = new FileStream(path, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
list = (List<Login>)bf.Deserialize(fs);
foreach (Login login in list)
{
this.comboBox1.Items.Add(login.Uid);
}
fs.Close();
if (this.comboBox1.Items.Count > 0)
{
this.comboBox1.SelectedIndex = 0;
}
ReFill();//填充密码记住密码复选框
}
private void ReFill()//填充密码文本框
{
string pwd = "";
string status = "";
string sql = "select password,status from Admin where name='" + this.comboBox1.Text + "'";
Con = new SqlConnection(strCon);
Con.Open();
SqlCommand sqlComm = new SqlCommand(sql, Con);
SqlDataReader sqlDR = sqlComm.ExecuteReader();
if (sqlDR.Read())
{
pwd = sqlDR[0].ToString();
status = sqlDR[1].ToString();
}
Con.Close();
if (status == "真")
{
this.checkBox1.Checked = true;
this.textBox1.Text = pwd;
}
else
{
this.checkBox1.Checked = false;
this.textBox1.Text = "";
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//checkbox
{
this.ReFill();
}
//登陆按钮代码
private void button1_Click(object sender, EventArgs e)
{
string strSelect = "select count(*) from Admin where name='"+this.comboBox1.Text+"' and password='"+this.textBox1.Text+"'";
Con = new SqlConnection(strCon);
try
{
Con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SqlCommand sqlComm = new SqlCommand(strSelect,Con);
try
{
if ((int)sqlComm.ExecuteScalar() == 1)
{
this.Hide();
MainForm mf = new MainForm();
mf.ShowDialog(); string path = "login.bin"; if (!File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
Login login = new Login();
login.Uid = this.comboBox1.Text;
list.Add(login);
bf.Serialize(fs, list);
fs.Close();
}
else
{
FileStream fs = new FileStream(path, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
list = (List<Login>)bf.Deserialize(fs);
bool f = true;
foreach (Login login in list)
{
if (login.Uid == this.comboBox1.Text)
{
f = true; break;
}
else
{
f = false;
}
}
fs.Close();
if (f == false)
{
fs = new FileStream(path, FileMode.Create);
bf = new BinaryFormatter();
Login login = new Login();
login.Uid = this.comboBox1.Text;
list.Add(login);
bf.Serialize(fs, list);
fs.Close();
}
}
string chk = this.checkBox1.Checked == true ? "真" : "假";
string strSel1 = "update Admin set status='" + chk + "' where name='" + this.comboBox1.Text + "' and password='" + this.textBox1.Text + "'"; ;
this.Close();
SqlCommand sqlComm1 = new SqlCommand(strSel1, Con);
sqlComm1.ExecuteNonQuery();//修改复选框的值
Con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void LoginForm_Load(object sender, EventArgs e)
{
string path = "login.bin";
if (!File.Exists(path))
{
return;
}
FileStream fs = new FileStream(path, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
list = (List<Login>)bf.Deserialize(fs);
foreach (Login login in list)
{
this.comboBox1.Items.Add(login.Uid);
}
fs.Close();
if (this.comboBox1.Items.Count > 0)
{
this.comboBox1.SelectedIndex = 0;
}
ReFill();//填充密码记住密码复选框
}
private void ReFill()//填充密码文本框
{
string pwd = "";
string status = "";
string sql = "select password,status from Admin where name='" + this.comboBox1.Text + "'";
Con = new SqlConnection(strCon);
Con.Open();
SqlCommand sqlComm = new SqlCommand(sql, Con);
SqlDataReader sqlDR = sqlComm.ExecuteReader();
if (sqlDR.Read())
{
pwd = sqlDR[0].ToString();
status = sqlDR[1].ToString();
}
Con.Close();
if (status == "真")
{
this.checkBox1.Checked = true;
this.textBox1.Text = pwd;
}
else
{
this.checkBox1.Checked = false;
this.textBox1.Text = "";
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//checkbox
{
this.ReFill();
}
//登陆按钮代码
private void button1_Click(object sender, EventArgs e)
{
string strSelect = "select count(*) from Admin where name='"+this.comboBox1.Text+"' and password='"+this.textBox1.Text+"'";
Con = new SqlConnection(strCon);
try
{
Con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
SqlCommand sqlComm = new SqlCommand(strSelect,Con);
try
{
if ((int)sqlComm.ExecuteScalar() == 1)
{
this.Hide();
MainForm mf = new MainForm();
mf.ShowDialog(); string path = "login.bin"; if (!File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
Login login = new Login();
login.Uid = this.comboBox1.Text;
list.Add(login);
bf.Serialize(fs, list);
fs.Close();
}
else
{
FileStream fs = new FileStream(path, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
list = (List<Login>)bf.Deserialize(fs);
bool f = true;
foreach (Login login in list)
{
if (login.Uid == this.comboBox1.Text)
{
f = true; break;
}
else
{
f = false;
}
}
fs.Close();
if (f == false)
{
fs = new FileStream(path, FileMode.Create);
bf = new BinaryFormatter();
Login login = new Login();
login.Uid = this.comboBox1.Text;
list.Add(login);
bf.Serialize(fs, list);
fs.Close();
}
}
string chk = this.checkBox1.Checked == true ? "真" : "假";
string strSel1 = "update Admin set status='" + chk + "' where name='" + this.comboBox1.Text + "' and password='" + this.textBox1.Text + "'"; ;
this.Close();
SqlCommand sqlComm1 = new SqlCommand(strSel1, Con);
sqlComm1.ExecuteNonQuery();//修改复选框的值
Con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (status == "真")
{
this.checkBox1.Checked = true;
this.textBox1.Text = pwd;
str=pwd
}
//登陆按钮代码
if (str!="") str=this.textBox1.Text ;
else str=MD5(this.textBox1.Text );
当然 如果变换comboBox1的SelectIndex时重新根据保存密码项设置str的值
这样就可以MD5了
if (str=="") str=MD5(this.textBox1.Text );
上面的写错了
如果是数据库里保存的 就取出来后MD5再赋值给str