我用的是c#+sql 2000,我链接数据库后用select语句查询时
SqlDataReader.read()的返回值总是flase,这是为何?请高手指点。
我的程序:
SqlConnection conn = new SqlConnection("server=SOLOMON;database=mydb;uid=sa;pwd=");
conn.Open();
string name =(string) textBox1.Text;//用户名
string pass = (string)textBox2.Text;//密码
string str = "select *from users where username = 'name' and password = 'pass'";
SqlCommand mySqlCommand = conn.CreateCommand();
mySqlCommand.CommandText = str;
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
//调用ExecuteReader()方法运行SELECT,返回一个SqlDataReader对象
try
{
if(mySqlDataReader.Read())
{
MessageBox.Show("合法用户","系统提示");
}
else MessageBox.Show("非法用户","系统提示");
mySqlDataReader.Close();
}
catch(SqlException e1){MessageBox.Show("数据库错误","系统提示");}