开发者

DataTable array

开发者 https://www.devze.com 2023-03-16 15:44 出处:网络
Is it possible to create (allocate memory) a DataTable array and then access it like that for instance:

Is it possible to create (allocate memory) a DataTable array and then access it like that for instance:

dt[0].NewRow();

If this i开发者_Python百科s possible how can I allocate memory?

Thanks, Sun

ps I m using C# 2.0


Why not to use DataSet instead array?

DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables.Add(new DataTable());
ds.Tables[0].NewRow();


Yes, of course you can:

int n = 10; // the number of datatables 
DataTable[] dtArray = new DataTable[n];
for(int i=0; i < n; i++)
    dtArray[i] = new DataTable("DataTable " + i);

if you don't know the number of DataTables in advance, you can use an expandable structure like a List<DataTable>:

List<DataTable> dtList = new List<DataTable>();
dtList.Add(new DataTable());
dtList.Add(new DataTable());
...

or, as suggested by Reniuz, you can use a DataSet


Try this:

DataRow dr = dt.NewRow();
dr[0] = "Sydney"; // or you could generate some random string.
dt.Rows.Add(dr);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号