如何将每个数据库单元格值放入数组 [英] how to put each database cell value into array

查看:78
本文介绍了如何将每个数据库单元格值放入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

com = new SqlCommand("select Message_State from Table_3 where Message_State='CR'", con);
            
Int32[] sam = new int[10];

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = com;
                    con.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        
                        sam[i] = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
                        TextBox2.Text = sam[i].ToString();
                        TextBox2.Text = Convert.ToString(ds.Tables[0].Rows.Count);
                            
                    }

            con.Close();
}




我能够获取所选列的计数,但是如何将每个单元格值放入数组中……请帮助.....




im able to get the count of the selected column but how do i put each cell value into an array... please help.....

推荐答案

您好,
我想您想将每条记录放入一个int数组中.如果是这样,则代码如下:

Hi,
I presume you want to put every record in an array of int. If so, this is the code:

Int32 [][] sam = new Int32[ds.Tables[0].Rows.Count][ds.Tables[0].Columns.Count]
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  for (int j=0;j<ds.tables[0].columns.count;j++)>
    sam[i][j] = Convert.ToInt32(ds.Tables[0].Rows[i][j]);



您需要定义二维数组,因为您具有记录列表.

希望对您有所帮助,
干杯.



You need to define a 2-dimentional array as you have a list of records.

I hope it will help,
Cheers.


为了获得包含单元格值的数组的数组,您需要这样的东西:

In order to get an array of arrays holding cell values you need something like this:

...
da.Fill(ds);

List<object> list = new List<object>();

foreach(DataRow row in  ds.Tables[0].Rows)
{
    List<object> cells = new List<object>();

    foreach (DataColumn col in ds.Tables[0].Columns)
    {
        cells.Add(row[col]);
    }
    list.Add(cells.ToArray());
}

return list.ToArray();



返回值vill具有这种类型的对象[] []



The returned value vill have this type object[][]


这篇关于如何将每个数据库单元格值放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆