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 啊.
(導出查詢語句沒有什麼問題.)謝謝.

解决方案 »

  1.   

    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;
      

  2.   

    string str = "select colA,colB,colC from tableA where 1=1"; 
    if (condition) 
                str += "and colD in (@role)"; 
      

  3.   

    明显是sql 有问题,
    1楼正解
      

  4.   

    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'"; 
    这样你下面如果还有条件的话。
      

  5.   

    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的
      

  6.   

    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'"; 
      

  7.   

    當我直接這樣寫的時候,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())
            {
    ... ...
      

  8.   

    我自己搞定了. 換了種寫法:
    if (condition)
      string str = "select colA,colB,colC from tableA where colA='xxx' and colD in (@role)";