2016-09-01 9 views
0

ありがとうございました。申し訳ありませんが、これはすでにカバーされています。あなたは、単一のクエリを実行するためのサンプルのために、あなたのクエリにINオペレータを実行することができプログラムで複数のSELECTクエリを最適に実行するにはどうすればよいですか?

 string[] ExampleInput = { "foo", "bar", "daily", "special" }; 
     int[] ReturnData = new int[ExampleInput.GetUpperBound(0)]; 

     SqlConnection sqlConnection1 = new SqlConnection("FooBar Connection String"); 
     SqlCommand cmd = new SqlCommand(); 
     SqlDataReader reader; 

     cmd.CommandType = CommandType.Text; 
     cmd.Connection = sqlConnection1; 

     for (int i = 0; i <= ExampleInput.GetUpperBound(0); i++) 
     { 
      cmd.CommandText = "SELECT [int] FROM [table] WHERE [string] = '" + ExampleInput[i] + "'; "; 
      sqlConnection1.Open(); 
      reader = cmd.ExecuteReader(); 

      if (reader.HasRows) 
      { 
       while (reader.Read()) 
        ReturnData[i] = reader.GetInt32(0); 
      } 

      reader.Close(); 
      sqlConnection1.Close(); 
     } 

答えて

0

:ここ

は、最適化されていない(と危険な)方法の一例です。私は、IDisposableインターフェイス(メモリから削除)を実装するアンマネージドオブジェクトに対して、usingブロックを使用するコードをいくつか変更しました。サンプルの場合(コメントを参照):

var ExampleInput = { "foo", "bar", "daily", "special" }; 
List<int> tempReturnData = new List<int>(); 

using (var sqlConnection1 = new SqlConnection("FooBar Connection String")) 
{ 
    using (var cmd = new SqlCommand()) 
    { 
     cmd.Connection = sqlConnection1; 

     // generate something like 'foo','bar','daily', 'special' into the string 
     string parameterValue = string.Concat("'", string.Join("','", ExampleInput), "'"); 

     // execute the single query 
     cmd.CommandText = string.Format("SELECT [int] FROM [table] WHERE [string] IN ({0});", parameterValue); 

     sqlConnection1.Open(); 

     using (var reader = cmd.ExecuteReader()) 
     { 
      // add all ements on the listof integers 
      while (reader.Read()) 
       tempReturnData.Add(reader.GetInt32(0));   
     } 

     reader.Close(); 
     sqlConnection1.Close(); 
    } 
} 

// get the final list of ints as array 
int[] ReturnData = tempReturnData.ToArray(); 
0

複数のクエリを実行してその結果を得る方法は複数あります。あなたの質問に含まれているアプローチは1つの方法ですが、接続を開いたままにして新しいコマンドを作成することをお勧めします。

SqlConnection,SqlCommandおよびSqlDataReaderのオブジェクトは使い捨てであるため、これらのオブジェクトはusing節を使用して正しく処理する必要があります。これらのオブジェクトを破棄すると、アンマネージリソースのクリーンナップが処理され、必要なコンポーネントがクローズされます。

ここには、既存の方法に基づいて更新されたコードがあります。

string[] ExampleInput = { "foo", "bar", "daily", "special" }; 
int[] ReturnData = new int[ExampleInput.GetUpperBound(0)]; 

using (SqlConnection sqlConnection1 = new SqlConnection("FooBar Connection String")) 
{ 
    sqlConnection1.Open(); 

    for (int i = 0; i <= ExampleInput.GetUpperBound(0); i++) 
    {   
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = sqlConnection1; 
      cmd.CommandText = "SELECT [int] FROM [table] WHERE [string] = '" + ExampleInput[i] + "'; "; 

      using (var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
        ReturnData[i] = reader.GetInt32(0); 
      } 
     }    
    } 
} 

代わりに、単一の文字列にクエリをconcatonateとSqlDataReaderののNextResultメソッドを使用することができます。

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

関連する問題