第1个回答 2013-07-11
其实用不着这么麻烦!可以设一个登陆按钮就可以了!以下是具体代码:
在C#中登陆按钮代码
private void button1_Click(object sender, System.EventArgs e)
{
switch(comboBox1.SelectedIndex)
{
case 0:
if(textBox1.Text==""||textBox2.Text=="")
{
MessageBox.Show("用户名和密码不能为空","提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
return;
}
else
{
try
{
OleDbConnection conn=db.createdb();
conn.Open();
OleDbCommand cmd=new OleDbCommand("select count(*) from user_info where user_id='"+textBox1.Text+"'and user_pwd='"+textBox2.Text+"'and beizhu='1'",conn);
int count=Convert.ToInt32(cmd.ExecuteScalar().ToString());
if(count>0)
{
Form2 form=new Form2();
form.Show();
this.Hide();
}
}
catch(Exception ex)
{
MessageBox.Show("错误"+ex.ToString(),"错误");
}
}
break;
default:
break;
}
登陆:
public class Users
{
private string username="";
private string password="";
public string UserName
{
get{return this.username;}
set{this.username=value;}
}
public string Password
{
get{return this.password;}
set{this.password=value;}
}
public string CheckUser()
{
SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["ConStr"]);
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="select UserName,Password from Users where UserName=@uname";
cmd.Parameters.Add("@uname",SqlDbType.VarChar,10);
cmd.Parameters[0].Value=UserName;
string pass="";
try
{
con.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
pass=reader[1].ToString();
}
else
{
reader.Close();
con.Close();
return UserName+"用户存在!";
}
reader.Close();
con.Close();
}
catch(SqlException er)
{
if(con.State==ConnectionState.Open)
con.Close();
return er.Message;
}
if(pass.Trim()!=Password.Trim())
{
return "对不起!"+UserName+"用户密码不正确!";
}
return "OK";
}
}