这是我的代码 - 我只需要将SQL DataSet转换为asp.net 4.5中的DataTable。我似乎无法弄明白。有很多帖子正在做相反的事情,但没有一个我能找到明确答案。
public static DataTable getGender()
{
DataTable DT = default(DataTable);
SqlConnection con = new SqlConnection(CnnString.ConnectionString.ToString());
SqlCommand cmd = new SqlCommand("ns_gender_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
try
{
//Fill the Dataset
da.Fill(ds, "Results");
DT = ds.Tables(0);
//**GOAL: I need to assign the DS.Table(0) to the DT (dataTable) so when this method is called it will return the table rows in the DT.
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
con = null;
}
return DT;
}
答案 0 :(得分:6)
实际上,DataSet
包含DataTables
的集合。你可以拿第一张桌子:
DataTable dataTable = ds.Tables[0];
另一方面,如果需要,您可以使用DataAdpter
填写DataTable
样本:
DataTable dt = new DataTable();
da.Fill(dt);
请参阅:
https://msdn.microsoft.com/library/system.data.dataset.tables(v=vs.110).aspx
http://www.dotnetperls.com/sqldataadapter
- 问题的原作者编辑(对于其他任何人看) 简单的解决方案终于打动了我 -
ds.Tables.Add(DT); // Code on how to add a table to a dataset!