我可以从sql获取多个数据类型记录到arraylist并以另一种形式获取它

时间:2019-02-26 15:12:27

标签: c# arrays sql-server winforms arraylist

我想在arraylist中获取任何表记录,并以另一种形式获取它(无论要循环的列数是多少,都将数据检索到arraylist中) 这是我的代码:

public static ArrayList getrecord(String Table, String Column, Int32 id)
{
    ArrayList asd = new ArrayList();
    Form1.con.Close();
    Form1.con.Open();
    SqlCommand sq = new SqlCommand("Select * from " + Table + " where " + Column + " = " + id, Form1.con);
    SqlDataReader sdr = sq.ExecuteReader();
    if (sdr != null)
    {
        while (sdr.Read())
        {
            for(int i =0; i < sdr.FieldCount; i++)
            {
                asd.Add(sdr.GetValue(i).ToString());
            }
        }
    }
    return asd;
}

1 个答案:

答案 0 :(得分:0)

我建议使用DataTable而不是ArrayList。您可以用所需的所有记录填充DataTable并以另一种形式使用它。

System.Data.SqlClient.SqlCommand sq = new System.Data.SqlClient.SqlCommand("your sql query", "your connection string");
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(sq);
sda.Fill(dt);