红蚂蚁与黑蚂蚁哪个好:c#中连接数据库,出现第 1 行: '=' 附近有语法错误。

来源:百度文库 编辑:高考问答 时间:2024/05/07 18:46:33
string conn="Data Source=localhost;Initial Catalog=zslDatabase;User ID=sa;password=sa";
string sele1 = "select * from 管理员 where 名称=user and 密码=zsl";
SqlCommand sqlSelectCommand1;
SqlConnection myconn = new SqlConnection (conn);
myconn.Open();
sqlSelectCommand1 = new SqlCommand(sele1,myconn);
SqlDataReader dr = sqlSelectCommand1.ExecuteReader();
if (dr.Read())
MessageBox.Show("密码正确");
else MessageBox.Show("密码或用户名错误");
dr.Close();

调试是在SqlDataReader dr = sqlSelectCommand1.ExecuteReader();行出现 第 1 行: '=' 附近有语法错误 的错误。

string sele1 = "select * from [管理员] where [名称]='user' and [密码]='zsl'";
修改sql语句如上,因为条件表达式中的user是字符串,所以要用单引号括起来zsl也是这样.

把所有的分号都去掉,这是VBScript,不是JAVAScript
修改后:
string conn="Data Source=localhost;Initial Catalog=zslDatabase;User ID=sa;password=sa"
string sele1 = "select * from 管理员 where 名称=user and 密码=zsl"
SqlCommand sqlSelectCommand1
SqlConnection myconn = new SqlConnection (conn)
myconn.Open()
sqlSelectCommand1 = new SqlCommand(sele1,myconn)
SqlDataReader dr = sqlSelectCommand1.ExecuteReader()
if (dr.Read())
MessageBox.Show("密码正确")
else MessageBox.Show("密码或用户名错误")
dr.Close()

没错,字符串一定要用 ‘’隔开

SQL语言最还别用中文的 就OK了

string sele1 = "select * from 管理员 where 名称='user' and 密码=zsl";