VS里的window做一个登录界面,那记住密码的功能要怎么写?代码在下面,但一执行就显示错误
namespace 测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//要先将User类先设为可以序列化(即在类的前面加[Serializable])
[Serializable]
public class User
{
private string userName;
public string Username
{
get { return userName; }
set { userName = value; }
}
private string passWord;
public string Password
{
get { return passWord; }
set { passWord = value; }
}
}
private void Form1_Load(object sender, EventArgs e)
{
// 读取配置文件寻找记住的用户名和密码
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
if (fs.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();
users = bf.Deserialize(fs) as Dictionary<string, User>;
foreach (User user in users.Values)
{
this.UserName.Items.Add(user.Username);
}
if (this.UserName.Items.Count > 0)
{
this.UserName.SelectedIndex = this.UserName.Items.Count - 1;
for (int i = 0; i < users.Count; i++)
{
if (this.UserName.Text != "")
{
if (users.ContainsKey(this.UserName.Text))
{
this.PassWord.Text = users[this.UserName.Text].Password;
this.RemeberPassword.Checked = true;
}
}
}
}
}
fs.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string constr = @"Data Source=DESKTOP-NAHV8AN;Initial Catalog=master;Integrated Security=True";
string sql = string.Format("select count(*) from users where Username='{0}' and Password='{1}'", UserName, PassWord);
//string sql = "select count(*) from login where username='" + name + "' and pwd='" + pwd + "'";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
object ob = cmd.ExecuteScalar();
con.Close();
if (Convert.ToInt32(ob) > 0)
{
MessageBox.Show("登录成功");
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
else MessageBox.Show("登录失败,用户名或密码错误!");
string username = this.UserName.Text.Trim();
string password = this.PassWord.Text.Trim();
User user = new User();
FileStream fs = new FileStream("data.txt", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
user.Username = username;
if (this.RemeberPassword.Checked) // 如果单击了记住密码的功能
{ // 在文件中保存密码
user.Password = password;
}
else
{ // 不在文件中保存密码
user.Password = "";
}
// 选在集合中是否存在用户名
if (users.ContainsKey(user.Username))
{
users.Remove(user.Username);
}
users.Add(user.Username, user);
//要先将User类先设为可以序列化(即在类的前面加[Serializable])
bf.Serialize(fs, users);
//user.Password = this.PassWord.Text;
fs.Close();
}
public Dictionary<string, User> users { get; set; }
private void UserName_SelectedIndexChanged(object sender, EventArgs e)
{
// 首先读取记住密码的配置文件
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
if (fs.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();
users = bf.Deserialize(fs) as Dictionary<string, User>;
for (int i = 0; i < users.Count; i++)
{
if (this.UserName.Text != "")
{
if (users.ContainsKey(UserName.Text) && users[UserName.Text].Password != "")
{
this.PassWord.Text = users[UserName.Text].Password;
this.RemeberPassword.Checked = true;
}
else
{
this.PassWord.Text = "";
this.RemeberPassword.Checked = false;
}
}
}
}
fs.Close();
}
}
}