2011-01-09 3 views
3

다음은 세션에서 여러 값을 가져 오려고했을 때의 ASPX 코드입니다. "쉼표 근처의 구문이 잘못되었습니다"(스 니펫의 라인을 표시) :쉼표 근처의 구문이 올바르지 않습니다.

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]), con); 
     SqlDataReader dr1 = cmd1.ExecuteReader(); 
     var yourlist =new List<Int32>(); 
     if (dr1.HasRows) 
     { 
      while (dr1.Read()) 
      { 
       yourlist.Add(Convert.ToInt32(dr1[0])); 
      } 
     } 

     //String str1 = String.Join(", ", yourlist.Select(o => o.ToString()).ToArray()); 
      dr1.Close(); 
     cmd1.Dispose(); 
     Array k= yourlist.ToArray(); 
     Int32 a =Convert.ToInt32(k.GetValue(0)); 
     Int32 b =Convert.ToInt32(k.GetValue(1)); 
     Int32 c =Convert.ToInt32(k.GetValue(2)); 
     Int32 d =Convert.ToInt32(k.GetValue(3)); 
     SqlCommand cmd2 = new SqlCommand("select id,name from plugins where id =(" + a + " or " + b + " or " + c + " or " + d + ")" , con); /// Error here? 
     SqlDataReader dr2 = cmd2.ExecuteReader(); ///Error here? 
     if (dr2.HasRows) 
     { 
      while (dr2.Read()) 
      { 
       ListBox2.DataSource = dr2; 
       ListBox2.DataBind(); 
      } 
     } 
     dr2.Close(); 
     cmd2.Dispose(); 
con.Close(); 

내가 무엇을 놓치고 나는 오류는 무엇입니까?

+3

질문에 대한 답변을 받아 들여야합니다. – SLaks

+1

최상의 대답을 받아 들여야합니다. – Kuncevic

답변

7

SQL 쿼리가 잘못되었습니다. 변경 :

SqlCommand cmd2 = new SqlCommand("select id,name from plugins 
where id in(" + a + " , " + b + " , " + c + " , " + d + ")" , con); 
2

이 줄에 오류가 있습니다. 시도해보십시오

SqlCommand cmd2 = new SqlCommand("select id,name from plugins where id =" + a + "or id =" + b + " or id =" + c + " or id =" + d + "" , con) 
+1

괄호는 필요 없습니다. – SLaks

+0

예. 원래 코드로되어 있으므로 계속 수행되었습니다. 지금 삭제되었습니다 :) –

1

여러 조건을 대신하여 귀하의 경우 또는 대신 SQL 쿼리에서 IN 절을 사용하는 것이 좋습니다. .. + "" + ... 향후에 동일하거나 유사한 오류를한다면 난 당신이 SQL 서버에서 직접 쿼리를 확인할 수 있습니다 String.Format

SqlCommand cmd2 = new SqlCommand(String.Format("select id,name from plugins where id IN ({0}, {1}, {2}, {3}", a, b, c, d,) con); 

또한 사용할 수 있습니다. 새 쿼리 창을 열고 SQL 쿼리를 복사 한 다음 실행하십시오. 이 경우 다음과 같이됩니다.

select id,name from plugins where id IN (1, 2, 3, 4) 
관련 문제