2013-11-20 14 views
15

結果を配列に保存したい場合、いくつかの結果を得るためにこのクエリを書きましたか? IF文でcol1とcol2の値を使用したいので、配列に保存することを考えています。SELECT SQLクエリの結果をC#Asp.netの配列に格納する

var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True"); 

using (con) 
using (var command = new SqlCommand("SELECT col1,col2 FROM some table", con)) 
{ 
    con.Open(); 
    command.ExecuteNonQuery(); 
} 

答えて

28

通常、私はこのためにクラスを使用しますことを

ClassName[] allRecords = null; 
string sql = @"SELECT col1,col2 
       FROM some table"; 
using (var command = new SqlCommand(sql, con)) 
{ 
    con.Open(); 
    using (var reader = command.ExecuteReader()) 
    { 
     var list = new List<ClassName>(); 
     while (reader.Read()) 
      list.Add(new ClassName { Col1 = reader.GetString(0), Col2 = reader.GetInt32(1) }); 
     allRecords = list.ToArray(); 
    } 
} 

注:あなたが本当に配列が必要な場合

public class ClassName 
{ 
    public string Col1 { get; set; } 
    public int Col2 { get; set; } 
} 

今あなたがリストとToArrayを埋めるためにループを使用することができます私は、最初の列がstringで、2番目がintegerであると推測しています。 C#が型保証されていることと、DataReader.GetXYメソッドをどのように使用するかを実証するだけです。

14

代わりに、任意のArrayあなたは次のようにDataTableにデータをロードすることができます。

あなたはまた後で、それぞれの行を反復処理し、同様に比較することができ

SqlDataAdapter da = new SqlDataAdapter(command); 
da.Fill(dt); 

のようなあなたのDataTableを埋めるためにSqlDataAdapaterを使用することができます

DataTable dt = new DataTable(); 
using (var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True")) 
{ 
    using (var command = new SqlCommand("SELECT col1,col2" + 
    { 
     con.Open(); 
     using (SqlDataReader dr = command.ExecuteReader()) 
     { 
      dt.Load(dr); 
     } 
    } 
} 

foreach (DataRow dr in dt.Rows) 
{ 
    if (dr.Field<string>("col1") == "yourvalue") //your condition 
    { 
    } 
} 
2

SQ L DATA READER:

この例では、代わりにListを使用しています。

try 
{ 
    SqlCommand comm = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",connection); 
    connection.Open(); 

    SqlDataReader reader = comm.ExecuteReader(); 
    List<string> str = new List<string>(); 
    int i=0; 
    while (reader.Read()) 
    { 
     str.Add(reader.GetValue(0).ToString()); 
    } 
    reader.Close(); 
} 
catch (Exception) 
{ 
    throw; 
} 
finally 
{ 
    connection.Close(); 
} 
1

かなり簡単:

public void PrintSql_Array() 
    { 
     int[] numbers = new int[4]; 
     string[] names = new string[4]; 
     string[] secondNames = new string[4]; 
     int[] ages = new int[4]; 

     int cont = 0; 

     string cs = @"Server=ADMIN\SQLEXPRESS; Database=dbYourBase; User id=sa; password=youpass"; 
     using (SqlConnection con = new SqlConnection(cs)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.Connection = con; 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = "SELECT * FROM tbl_Datos"; 
       con.Open(); 

       SqlDataAdapter da = new SqlDataAdapter(cmd); 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       foreach (DataRow row in dt.Rows) 
       { 
        numbers[cont] = row.Field<int>(0); 
        names[cont] = row.Field<string>(1); 
        secondNames[cont] = row.Field<string>(2); 
        ages[cont] = row.Field<int>(3); 

        cont++; 
       } 

       for (int i = 0; i < numbers.Length; i++) 
       { 
        Console.WriteLine("{0} | {1} {2} {3}", numbers[i], names[i], secondNames[i], ages[i]); 
       } 

       con.Close(); 
      } 
     } 
    } 
1

言及されていない偉大な選択肢がテーブルにあるオブジェクトを使用するエンティティフレームワークを使用することです - あなたができる配列にデータを取得しますEntity Frameworkの使用を開始する上での情報のため

var rows = db.someTable.SqlQuery("SELECT col1,col2 FROM someTable").ToList().ToArray(); 

https://msdn.microsoft.com/en-us/library/aa937723(v=vs.113).aspx

を参照してくださいのようなものを行います
関連する問題