C# 如何在C中将数据集转换为数据表#

C# 如何在C中将数据集转换为数据表#,c#,sql-server,ado.net,C#,Sql Server,Ado.net,这是我的代码——我只需要在asp.net 4.5中将SQL数据集转换为DataTable。我似乎不明白。有许多帖子做了相反的事情,但我找不到一个能明确回答问题的帖子 public static DataTable getGender() { DataTable DT = default(DataTable); SqlConnection con = new SqlConnection(CnnString.ConnectionString.ToString());

这是我的代码——我只需要在asp.net 4.5中将SQL数据集转换为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;
}
实际上,a包含一个。您可以选择第一张桌子:

DataTable dataTable = ds.Tables[0];
另一方面,如果需要,您可以使用
DataAdpter
来填充
DataTable
,例如:

DataTable dt = new DataTable();
da.Fill(dt);
更多信息,请参阅:

--问题原始作者的编辑(供其他人查看) 我终于想到了这个简单的解决办法--

实际上,a包含一组。您可以选择第一张桌子:

DataTable dataTable = ds.Tables[0];
另一方面,如果需要,您可以使用
DataAdpter
来填充
DataTable
,例如:

DataTable dt = new DataTable();
da.Fill(dt);
更多信息,请参阅:

--问题原始作者的编辑(供其他人查看) 我终于想到了这个简单的解决办法--


你试过把桌子还回去吗?而不是DT=ds.表[0];使用返回ds.表[0];谢谢我终于找到了它(就在发布这个!!)ds.Tables.Add(DT);你试过把桌子还回去吗?而不是DT=ds.表[0];使用返回ds.表[0];谢谢我终于找到了它(就在发布这个!!)ds.Tables.Add(DT);我找到了一个解决方案--ds.Tables.Add(DT);--太容易了,几乎要疼了。谢谢我找到了一个解决方案--ds.Tables.Add(DT);--太容易了,几乎要疼了。谢谢