//登录事件处理方法
private void button1_Click(object sender, EventArgs e)
{
try
{
string sql = string.Format("select uname,password,userrole from tb_userinfo where uname={0}", this.tb_name.Text.Trim());
DataSet ds = sqlHple.GetDateSet(sql);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
//判断当前用户登录进来的密码是否与传进来的密码相等
if (ds.Tables[0].Rows[0]["uname"].ToString().Equals(this.tb_pwd.Text.Trim()))
{
//对用户名和用户角色进行赋值
Program.username = ds.Tables[0].Rows[0]["uname"].ToString();
Program.userrole = ds.Tables[0].Rows[0]["userrole"].ToString();
//是否显示主窗体
Program.isshowmina = true;
this.Close();
}
else
{
MessageBox.Show("用户密码输入错误");
}
}
else
{
MessageBox.Show("用户名输入错误");
return;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
这个是sqlHple.GetDateSet里面的方法
public static DataSet GetDateSet(string sql)
{
SqlConnection con = new SqlConnection(strcon);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(strcon, con);
sda.Fill(ds);
return ds;
}
select uname,password,userrole from tb_userinfo where uname={0}中的uname是字符类型应该加单引号 uname='{0}'