C#查询数据库提示列名无效是什么意思

如题所述

列名写错了,或者sql语句里面有语法错误,检查下,看看追问

代码为
string sql=string.format("update teacher set sex={0} where teachername={1}",sex,cb.text);
sqlcommand command=new sqlcommand(sql,connection);
int result=0;
result=command.executenonquery();
列名没错误,请问是什么问题呢

追答

能把string sql=string.format("update teacher set sex={0} where teachername={1}",sex,cb.text);的执行结果 sql字符串的内容贴出来吗??,用参数查询我们一般不用format,format只是简单处理格式,{0}和替换成sex的值,但不会给你加上 ' '号,sql语句可能就错了,带参数的sql查询,一般用SqlParameter这个对象,你查查,给你个例子,
SqlParameter[] mysqlpara1 = new SqlParameter[2];
SqlParameter[] mysqlpara2 = new SqlParameter[3];

mysqlpara1[0] = new SqlParameter("@userid",SqlDbType.NChar,10);
mysqlpara1[0].Value = txtUserID.Text;

mysqlpara1[1] = new SqlParameter("@passwd",SqlDbType.NChar,10);
mysqlpara1[1].Value = txtPassWord.Text;

strSql=@"select count(*) from userauth where userid=@userid and passwd=@passwd";

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-02-27
你写的SQL语句里的列名和数据库的列名不符。。追问

列名检查过了没错误,代码在上面,能不能帮我看下哪里有问题,谢啦

追答

那句SQL语句改成update teacher set sex=‘{0}’ where teachername=‘{1}’
然后再试试

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网