2013-02-08 2 views
0

응용 프로그램에서 작업 중이므로 데이터베이스에서 일부 데이터를 검색해야합니다. 다음 쿼리를 사용하고 있습니다.SQL 쿼리는 SQL Server 2008에서 작동하지만 C#에서는 작동하지 않습니다.

SELECT DISTINCT Context.ContextId, ContextName 
FROM Context 
INNER JOIN RunInstance 
    ON Context.ContextId IN 
     (SELECT RunInstance.ContextId 
     FROM RunInstance 
     INNER JOIN ProcessInstance 
      ON RunInstance.RunInstanceId 
      IN (SELECT RunInstanceId FROM ProcessInstance 
       WHERE RiskDate = '2010-08-20')); 

이 쿼리는 2008 년

그러나, 나는 그것이 나에게 어떤 출력이 표시되지 내 C# 응용 프로그램에 넣을 때 SQL 서버에서 완벽하게 작동합니다.

내 코드 :

string squery = @"SELECT DISTINCT Context.ContextId, ContextName FROM Context INNER JOIN RunInstance ON Context.ContextId IN 
    (Select RunInstance.ContextId From RunInstance 
    INNER JOIN ProcessInstance ON RunInstance.RunInstanceId 
    IN (SELECT RunInstanceId FROM ProcessInstance Where 
    RiskDate = '2010-08-20')); "; 

using(SqlConnection sqcon = new SqlConnection("Data Source=WMLON-Z8-SQL20,61433;Initial Catalog=statusdb;Integrated Security=True")){ 
    sqcon.Open(); 
    using(SqlCommand command = new SqlCommand(squery,sqcon)) 
     using(SqlDataReader reader = command.ExecuteReader()){ 
      while(reader.Read()){ 
       Console.WriteLine(reader[0]+"\t"+reader[1]); 
      } 
     } 
}  

사람이 문제가 무엇인지 말씀해 주시겠습니까?

+0

"사용중"줄에 백틱이 있습니까? – mbeckish

+2

중단 점을 설정하고 줄마다 코드를 단계별로 실행 해 보았습니까? – mbeckish

+0

@mbeckish 예. 시도했습니다. 그것은 내 whil 루프에가는 것이 아니라 쿼리가 실행 중임을 의미하지만, 화면에 아무 것도 표시하지 않습니다. – shyam

답변

0

CommandTextCommandType에 넣고 사용해보십시오.

using(SqlConnection sqcon = new SqlConnection(
"Data Source=WMLON-Z8-SQL20,61433;Initial Catalog=statusdb;Integrated Security=True")) 
{ 
    using(SqlCommand command = new SqlCommand(squery,sqcon)){ 
     command.CommandType = CommandType.Text; 
     sqcon.Open(); 
     using(SqlDataReader reader = command.ExecuteReader()) 
     { 
      while(reader.Read()){ 
      Console.WriteLine(reader[0]+"\t"+reader[1]); 
      } 
     } 
     sqcon.Close(); 
    } 
} 

-- Using EXISTS or IN could improve your query 
    SELECT DISTINCT 
            Context.ContextId , 
            ContextName 
    FROM    Context 
            INNER JOIN RunInstance ON Context.ContextId = RunInstance.ContextId  
            INNER JOIN ProcessInstance ON RunInstance.RunInstanceId = ProcessInstance.RunInstanceId 
    WHERE RiskDate = '2010-08-20' 
+0

어디에 넣으시겠습니까? – shyam

+0

답변을 업데이트했습니다. – Win

+0

CommandType.Text에 오류가 있습니다. – shyam

관련 문제