sql执行问题,vb.net转c# 的代码

afqitx 2012-02-08 06:17:42

command = new SqlCommand("exec('Select * From test Where id like @id)')", conn);
command.Parameters.AddWithValue("id", "1,2,3");
command.ExecuteNonQuery();

总提示@id未定义
同样的代码vb.net下没有任何错误,一切正为

command = new SqlCommand("exec('Select * From test Where id like @id)')", conn)
command.Parameters.AddWithValue("id", "1,2,3")
command.ExecuteNonQuery()
...全文
69 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
afqitx 2012-02-22
  • 打赏
  • 举报
回复
我也奇怪,有空仔细找找
  • 打赏
  • 举报
回复
exec不能这样用,你可以把exec('Select * From test Where id like @id)')放在sql server manageMent studio里执行,报一样的错误。
exec里的参数只能是用在有存储过程的时候,如
exec my_stored_procedure @myparam = 'abc'

vb.net允许这样做,我觉得设计有问题。
afqitx 2012-02-08
  • 打赏
  • 举报
回复
另为,如果把commandtext改为Select * From tablefieldList Where id in @id就没有任何问题
afqitx 2012-02-08
  • 打赏
  • 举报
回复
like 与in是抄的时候错误,
command = new SqlCommand("exec('Select * From test Where id IN (" + x + "))')", conn);
就不不想拼,用参数的话就会出错
command = new SqlCommand("exec('Select * From test Where id in @id)')", conn);
command.Parameters.AddWithValue("@id", "1,2,3");
会有错误
必须声明标量变量 "@id"
调试看了,command的commandtext值 为exec('Select * From tablefieldList Where id in @id')
v = command.Parameters[0].Value.ToString();
k = command.Parameters[0].ParameterName;
v为1,2,3
k为@id都没错
孟子E章 2012-02-08
  • 打赏
  • 举报
回复
command.Parameters.AddWithValue("id", "1,2,3");
改成
command.Parameters.AddWithValue("@id", "1,2,3");
试试

另外 如果你的id是数字类型的,应该写成
String x = "1,2,3";
command = new SqlCommand("exec('Select * From test Where id IN (" + x + "))')", conn);

而不是Like

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧