本人新手菜鸟,最近因为学习需要,编写了一个登录界面。这个对于一些大牛来说是非常简单的,但是对于新手来说,还是有点参考意见。本文参考了一些文章,但是看的太多记不大清楚出处,在此表示歉意。本文若有什么不对的地方或者需要改进的地方,还请各位帮忙指正,小弟在此谢过。。。废话不多说,开始正题。
登录界面如上图所示,第一个combobox的Name是cb_useName,第二个text的Name是T_password.此程序包含的access数据库放在该程序的debug文件夹中,因为对连接数据库的字符串的方法还没去领会,所以暂时采用这种比较简单的连接方式。。。。代码如下
private void button1_Click(object sender, EventArgs e)
{
if(cb_useName.Text.Length==0)
{
MessageBox.Show("用户名不能为空...");
}
else if(T_passWord.Text.Length==0)
{
MessageBox.Show("密码不能为空...");
}
else
{
string userName = cb_useName.Text.Trim();
string passWord = T_passWord.Text.Trim();
string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Liangcang.accdb";
OleDbConnection con = new OleDbConnection(conStr);
con.Open();
string cmdStr = "select * from Users where userName='" + this.cb_useName.Text + "'and Password='" + this.T_passWord.Text + "'";
OleDbCommand cmd = new OleDbCommand(cmdStr, con);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read()) //如果找到用户信息,说明登录成功
{
MessageBox.Show("登录成功!");
MainForm newForm = new MainForm();
newForm.Show(); //弹出主窗体
this.Hide(); //隐藏用户登录窗体
}
else
{
MessageBox.Show("用户名或密码错误!");
}
reader.Close();
//cmd.Connection.Close();
con.Close();
}
}`
private void button2_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//密码显示框勾选,显示明文
T_passWord.PasswordChar = new char();
}
else
{
//密码显示框未勾选,显示密文
T_passWord.PasswordChar = '*';
}
}