Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

c# 4.0 - how to store multiple DataTables into single DataSet in c#?

I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.

MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);

MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);

MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);

MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...

If you want your tables named in the DataSet you can create your tables from the DataSet

DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1");  // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name

You can also set the TableName property of the tables if you use the first approach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...