111,125
社区成员
发帖
与我相关
我的任务
分享 //登入功能
private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "" || textBox1.Text == "")
{
MessageBox.Show("请输入完整用户名和密码", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
DataClass.SQLConn.Close();
SqlCommand seleltUserinfocomm = new SqlCommand("select * from UserTable where UserID=@USERID and UPassWord=@PASSWORD",DataClass.SQLConn);
seleltUserinfocomm.Parameters.Add(new SqlParameter("@USERID", SqlDbType.VarChar, 10));
seleltUserinfocomm.Parameters["@USERID"].Value = comboBox1.Text;
seleltUserinfocomm.Parameters.Add(new SqlParameter("@PASSWORD", SqlDbType.VarChar, 10));
seleltUserinfocomm.Parameters["@PASSWORD"].Value = textBox1.Text;
DataClass.SQLConn.Open();
SqlDataReader myUinfoRe = seleltUserinfocomm.ExecuteReader();
if (myUinfoRe.Read())
{
this.Hide();
ID = myUinfoRe.GetString(0).ToString();
Password = myUinfoRe.GetString(1).ToString();
role = (bool)myUinfoRe.GetSqlBoolean(2);
if (role == true)
{
Role = "管理员";
}
else
{
Role = "普通用户";
}
UName = myUinfoRe.GetString(3).ToString();
login = true;
//MainForm mainform = new MainForm();
//mainform.ShowDialog();
}
else
{
MessageBox.Show("用户名字或者密码错误");
}
DataClass.SQLConn.Close();
myUinfoRe.Close();
}
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if (txtUserName.Text == "" || txtUserPwd.Text == "")
{
MessageBox.Show("用户名或密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
string name = txtUserName.Text.Trim();
string pwd = txtUserPwd.Text.Trim();
string ConnectionString = "server=localhost;database=MYdb;User=sa;Pwd=sa";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tb_User where UserName='" + name + "' and UserPwd='" + pwd + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
{
cmd.ExecuteNonQuery();
conn.Close();
this.Hide();
frmMain Main = new frmMain();
Main.Show();
}
else
{
txtUserName.Text = "";
txtUserPwd.Text = "";
MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'";
try
{
SqlCommand command = new SqlCommand(sql, "数据库连接connection,楼主自己写");
connection.Open();//楼主自己创建connection对象
if(command.ExecuteScalar()==null)
{
用户名或密码错误
}
else
{
登录主界面
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "数据库操作-登录");
}
finally
{
connection.Close();
}
}