c#中如何用SqlDataReader提取数据库中的数据?
主要的代码段:privatevoidbutton1_Click(objectsender,EventArgse){try{this.sqlConnection1.Open...
主要的代码段: private void button1_Click(object sender, EventArgs e) { try
{
this.sqlConnection1.Open();//打开数据库连接
this.sqlCommand1.Parameters["@username"].Value = this.txtName.Text;//赋值给sql语句中@username
this.sqlCommand1.Parameters["@password"].Value = this.txtPwd.Text;
SqlDataReader reader = this.sqlCommand1.ExecuteReader() ;
int i = int.Parse(this.sqlCommand1.ExecuteScalar().ToString());//查询并返回查询结果集中第一行的第一列忽略其他列
if (i>0)
{
MainForm mform = new MainForm();
AdminMainForm admform = new AdminMainForm(); if (reader.Read()) {
if (reader.GetString(3) == "002")//登录权限普通用户 {
this.Hide(); mform.Show();
}
else if (reader.GetString(3) == "001")//超级用户
{
this.Hide();
admform.Show();
}
reader.Close();
}
}
else
{
MessageBox.Show("用户名或密码错误");
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.sqlConnection1.Close();//关闭数据库连接
}
}
} 展开
{
this.sqlConnection1.Open();//打开数据库连接
this.sqlCommand1.Parameters["@username"].Value = this.txtName.Text;//赋值给sql语句中@username
this.sqlCommand1.Parameters["@password"].Value = this.txtPwd.Text;
SqlDataReader reader = this.sqlCommand1.ExecuteReader() ;
int i = int.Parse(this.sqlCommand1.ExecuteScalar().ToString());//查询并返回查询结果集中第一行的第一列忽略其他列
if (i>0)
{
MainForm mform = new MainForm();
AdminMainForm admform = new AdminMainForm(); if (reader.Read()) {
if (reader.GetString(3) == "002")//登录权限普通用户 {
this.Hide(); mform.Show();
}
else if (reader.GetString(3) == "001")//超级用户
{
this.Hide();
admform.Show();
}
reader.Close();
}
}
else
{
MessageBox.Show("用户名或密码错误");
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.sqlConnection1.Close();//关闭数据库连接
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询