0

私はアプリケーションに取り組んでおり、データベースからいくつかのデータを取得する必要があります。次のクエリを使用しています。

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

このクエリは、SQLServer2008で完全に機能します。

ただし、C#アプリケーションに配置すると、出力が表示されません。

私のコード:

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]);
            }
        }
}     

誰かが問題が何であるか教えてもらえますか?

4

1 に答える 1

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'
于 2013-02-08T20:17:06.187 に答える