注册 登录
编程论坛 C# 论坛

C#中查询语句中的数据类型

ybh24138227 发布于 2015-12-08 22:07, 2570 次点击
                string s="select yw from 移位密码 where sz=d";
                SqlConnection ds1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);

                ds1.Open();
                SqlCommand bs1 = new SqlCommand(ssql2, ds1);
               
                string str1 = Convert.ToString(bs1.ExecuteScalar());
                ds1.Close();
    其中移位密码表中yw是varchar类型,sz是int类型运行的着色部分显示列名 'd' 无效。请问该怎么解决啊!
    求各位大哥帮助
3 回复
#2
over12302015-12-09 08:25
sz是int型,那d就应该是个变量了,变量就不要加到引号里面去了.
int  d=3;
string s="select yw from 移位密码 where sz=" + d;

不用变量的话,就是
string s="select yw from 移位密码 where sz=3";
#3
JLennon2015-12-09 09:39
“select yw from 移位密码 where sz=d”//这个是sqlcommand语句,你这样写传入sql server就是select yw from 移位密码 where sz=d,无法执行的
”select yw from 移位密码 where sz=“+d//加号是连接字符串的,这样传入sql server的就是select yw from 移位密码 where sz=3
#4
PlatoEternal2015-12-12 13:51
"select yw from 移位密码 where sz=d”
错了 应该是

"select yw from 移位密码 where sz='d'”

1