if 語句中的 SqlParameter

wendy_fw 2009-03-25 05:15:12
string str = "select colA,colB,colC from tableA ";
if (condition)
str += "and colD in (@role)";

SqlParameter[] param = {
new SqlParameter ("@role",SqlDbType.NVarChar,300)};
param[0].Value = "'a','b'";

當執行ExecuteReader 時為什麼 SqlDataReader.Read()為false 啊.
(導出查詢語句沒有什麼問題.)

謝謝.
...全文
74 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wendy_fw 2009-03-27
  • 打赏
  • 举报
回复
我自己搞定了. 換了種寫法:
if (condition)
string str = "select colA,colB,colC from tableA where colA='xxx' and colD in (@role)";
wendy_fw 2009-03-26
  • 打赏
  • 举报
回复
當我直接這樣寫的時候,dr.Read()就是True 哦,
一旦想通過參數去執行,dr.Read()就是False.
真的不知道怎麼回事啊

 string str = "select colA,colB,colC from tableA where colA='xxx'";
if (condition)
str += " and colD in ('a','b')";

//SqlParameter[] param = {
// new SqlParameter ("@role",SqlDbType.NVarChar,300)};
//param[0].Value = "'a','b'";
SqlDataReader dr = SQLUtility.ExecuteReader(SQLUtility.ConnMenu, CommandType.Text, str, parm);

List<RDTreeNode> treeNode = new List<RDTreeNode>();
while (dr.Read())
{
... ...
crazyminds110 2009-03-26
  • 打赏
  • 举报
回复
查你数据库中的数据是否有符合你要查询的结果
如果没有语法错误,可能你的错误出现在你的查询条件上
检查
wendy_fw 2009-03-26
  • 打赏
  • 举报
回复
string str = "select colA,colB,colC from tableA where colA='xxx'";
if (condition)
str += " and colD in (@role)";

SqlParameter[] param = {
new SqlParameter ("@role",SqlDbType.NVarChar,300)};
param[0].Value = "'a','b'";
wendy_fw 2009-03-26
  • 打赏
  • 举报
回复
string str = "select colA,colB,colC from tableA ";
if (condition)
str += "where colD in (@role)";

SqlParameter[] param = {
new SqlParameter ("@role",SqlDbType.NVarChar,300)};
param[0].Value = "'a','b'";

不好意思, 是我打錯了,應該是where的
  • 打赏
  • 举报
回复
string str = "select colA,colB,colC from tableA where 1=1 ";
if (condition)
str += "and colD in (@role)";

SqlParameter[] param = {
new SqlParameter ("@role",SqlDbType.NVarChar,300)};
param[0].Value = "'a','b'";


这样你下面如果还有条件的话。
Adechen 2009-03-25
  • 打赏
  • 举报
回复
明显是sql 有问题,
1楼正解
xsm545 2009-03-25
  • 打赏
  • 举报
回复
少了where关键字!顶
wuyq11 2009-03-25
  • 打赏
  • 举报
回复
string str = "select colA,colB,colC from tableA where 1=1";
if (condition)
str += "and colD in (@role)";
Jcomet 2009-03-25
  • 打赏
  • 举报
回复
少了where关键字!
mqc507 2009-03-25
  • 打赏
  • 举报
回复
逗··· 真逗 ···· 真的很逗 ····
xz_xq 2009-03-25
  • 打赏
  • 举报
回复
楼上正解
cppfaq 2009-03-25
  • 打赏
  • 举报
回复
SQL 拼的有问题
SqlCommand command = new SqlCommand();
string str = "select colA,colB,colC from tableA ";
if (condition)
{
str += "where colD in (@role)";

SqlParameter param = new SqlParameter ("@role",SqlDbType.NVarChar,300);
command.Parameters.Add(param).Value = "'a','b'";
}
command.CommandText = str;

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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