如何从sql中选择多个列并将其存储在单个数组中

时间:2013-07-17 08:44:34

标签: c# asp.net sql-server

     SqlDataAdapter da = new SqlDataAdapter("select d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25,d26,d27,d28,d29,d30, name from jully  where batch=" + "'" + s_batch + "'" +
    "and semester=" + "'" + s_semester + "'" + "and shift=" + "'" + s_shift + "'"+"and rolno="+rolno, conn);
     DataTable dt = new DataTable();
     conn.Open();
     da.Fill(dt);

     for (int i = 0; i < dt.Columns.Count; i++)
     {
        hhh[i] = dt.Columns[].ToString();
     }

3 个答案:

答案 0 :(得分:1)

根据您对hhh类型的期望,您可以执行类似

的操作
hhh = dt.AsEnumerable().ToArray();

会给你一个DataRows数组

hhh = dt.AsEnumerable().Select(row => row.ItemArray).ToArray();

会给你一个锯齿状的数组 - 一个object数组的数组,每行一个数组

答案 1 :(得分:0)

     SqlDataAdapter da = new SqlDataAdapter("select d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25,d26,d27,d28,d29,d30, name from jully  where batch=" + "'" + s_batch + "'" +
        "and semester=" + "'" + s_semester + "'" + "and shift=" + "'" + s_shift + "'"+"and rolno="+rolno, conn);
         DataTable dt = new DataTable();
         conn.Open();
         da.Fill(dt);

         for (int i = 0; i < dt.rows.Count; i++)
         { for (int j = 0; j < dt.columns.Count; j++)
            hhh[k] = dt.rows[i][j].tostring();
k++;
         }

答案 2 :(得分:0)

试试这个:

string[] hhh = new string[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
    hhh[i] = dt.Columns[i].ToString();
}