bool CheckUser()
{
bool flag = false;//默认标识符为false
string strConnection = "server=.;uid=sa;pwd=123456;database=TestDB";//固定类型
SqlConnection sc = new SqlConnection(strConnection);//初始化connection 连接对象
sc.Open();//打开数据库
SqlCommand scomm = new SqlCommand();//初始化SQL命令对象
//SQL命令
scomm.CommandText = "select * from admin where username ='" + comboBox1.SelectedItem.ToString() + "' and password='" + maskedTextBox1.Text + "'";
scomm.CommandType = CommandType.Text;//SQL执行类型
scomm.Connection = sc;//命令执行所在的connection
SqlDataReader rd = scomm.ExecuteReader();//开始执行语句
if (rd.HasRows)//判断reader游标是否有行数
{
flag = true;//标识符置为true
}
rd.Close();//关闭read游标
sc.Close();//关闭connection连接
return flag;
}
下载