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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8983277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:26:15  来源:igfitidea点击:

How to convert DataSet to DataTable

c#asp.nethtmlsql

提问by AlwaysANovice

I am retrieving data from a SQL table so I can display the result on the page as a HTML table. Later I need to be able to save that table as a CSV file.

我正在从 SQL 表中检索数据,以便可以将结果作为 HTML 表显示在页面上。稍后我需要能够将该表保存为 CSV 文件。

So far I have figured out how to retrieve the data and fill them in a dataset for display purpose (which is working perfectly)...

到目前为止,我已经想出了如何检索数据并将它们填充到数据集中用于显示目的(工作完美)...

        string selectQuery = "SELECT Name, ProductNumber, ListPrice FROM Poduction.Product";

        // Establish the connection to the SQL database 
        SqlConnection conn = ConnectionManager.GetConnection();
        conn.Open();

        // Connect to the SQL database using the above query to get all the data from table.
        SqlDataAdapter myCommand = new SqlDataAdapter(selectQuery, conn);

        // Create and fill a DataSet.
        DataSet ds = new DataSet();
        myCommand.Fill(ds);

and how to save them in a CSV file with the help of following code from: http://www.evontech.com/login/topic/1983.html

以及如何借助以下代码将它们保存在 CSV 文件中:http: //www.evontech.com/login/topic/1983.html

    private void exportDataTableToCsv(DataTable formattedDataTable, string filename)
    {
       DataTable toExcel = formattedDataTable.Copy();
       HttpContext context = HttpContext.Current;
       context.Response.Clear();

       foreach (DataColumn column in toExcel.Columns)
       {
          context.Response.Write(column.ColumnName + ",");
       }

       context.Response.Write(Environment.NewLine);
       foreach (DataRow row in toExcel.Rows)
       {
          for (int i = 0; i < toExcel.Columns.Count; i++)
          {
             context.Response.Write(row.ToString().Replace(",", string.Empty) + ",");
          }

          context.Response.Write(Environment.NewLine);
       }

       context.Response.ContentType = "text/csv";
       context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
       context.Response.End();
    }

Now my problem is how do I convert this DataSetto DataTable? I have tried the way described here with NOluck: http://www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspx

现在我的问题是如何将其转换DataSetDataTable?我尝试过这里描述的方法,但没有运气:http: //www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspx

Can anyone help me?

谁能帮我?

采纳答案by Jon Skeet

A DataSetalready containsDataTables. You can just use:

ADataSet已经包含DataTables. 你可以只使用:

DataTable firstTable = dataSet.Tables[0];

or by name:

或按名称:

DataTable customerTable = dataSet.Tables["Customer"];

Note that you should have usingstatements for your SQL code, to ensure the connection is disposed properly:

请注意,您应该using为 SQL 代码编写语句,以确保正确处理连接:

using (SqlConnection conn = ...)
{
    // Code here...
}

回答by Pranay Rana

DataSet is collection of DataTables.... you can get the datatable from DataSet as below.

DataSet 是 DataTables 的集合....您可以从 DataSet 中获取数据表,如下所示。

//here ds is dataset
DatTable dt = ds.Table[0]; /// table of dataset

回答by MORFEE89

Here is my solution:

这是我的解决方案:

DataTable datatable = (DataTable)dataset.datatablename;