关于data.read的问题,谢谢
private void Button1_Click(object sender, System.EventArgs e){
//用户登陆
// 在此处放置用户代码以初始化页面
String strConn="server=localhost;database=userinfo;user=sa;password=123456";
SqlConnection DBConn=new SqlConnection(strConn);
//打开数据库
DBConn.Open();
//建立一个SQL命令
SqlCommand cmd=new SqlCommand();
//建立命令执行SQL
cmd.CommandText="select * from userinfo where username='" +username.Text+"' and password='"+password.Text+"'";
//命令与数据库连接
cmd.Connection=DBConn;
//命令执行结果装在SqlDataReader容器中
SqlDataReader data=cmd.ExecuteReader();
//查询容器结果为真,检查到数据时
if(data.Read())
{
Response.Write("成功登陆");
}
else{
Response.Write("登陆失败");
}
//关闭数据库连接
DBConn.Close();
//释放数据库资源
DBConn.Dispose();
}
这是我的一个登陆的代码,判断的时候“if(data.Read())”明明可以返回数据,可是语句还是给我返回一个false。
我用GridView1.DataSource = data;
GridView1.DataBind();
能够把语句输出。
是不是我没理理解data.read(),我没有查到先关的资料。请大侠们指教。 --------------------编程问答-------------------- 如果存在多个行,则为 true;否则为 false。
看帮助 --------------------编程问答-------------------- cmd.CommandText="select * from userinfo where username='" +username.Text+"' and password='"+password.Text+"'";
这句话写完之后 你也没command绑定啊 --------------------编程问答--------------------
--------------------编程问答-------------------- //建立一个SQL命令
//建立一个SQL命令
SqlCommand cmd=new SqlCommand();
//建立命令执行SQL
cmd.CommandText="select * from userinfo where username='" +username.Text+"' and password='"+password.Text+"'";
cmd.CommandType = CommandType.Text;
//命令与数据库连接
cmd.Connection=DBConn;
//命令执行结果装在SqlDataReader容器中
SqlDataReader data=cmd.ExecuteReader();
//查询容器结果为真,检查到数据时
if(data.Read())
{
Response.Write("成功登陆");
}
else{
Response.Write("登陆失败");
}
SqlCommand cmd=new SqlCommand();
//建立命令执行SQL
cmd.CommandText="select * from userinfo where username='" +username.Text+"' and password='"+password.Text+"'";
cmd.CommandType = CommandType.Text;
//命令与数据库连接
cmd.Connection=DBConn;
//命令执行结果装在SqlDataReader容器中
SqlDataReader data=cmd.ExecuteReader();
//查询容器结果为真,检查到数据时
if(data.Read())
{
Response.Write("成功登陆");
}
else{
Response.Write("登陆失败");
}
补充:.NET技术 , C#