如何将DataSet转换为DataTable [英] How to convert DataSet to DataTable
问题描述
我从SQL表中检索数据,以便可以将结果作为HTML表格显示在页面上。后来我需要能够将该表格保存为CSV文件。
到目前为止,我已经想出了如何检索数据并将其填充到数据集中以用于显示目的(这是完美的)...
string selectQuery =SELECT Name,ProductNumber,ListPrice FROM Poduction.Product;
//建立与SQL数据库的连接
SqlConnection conn = ConnectionManager.GetConnection();
conn.Open();
//使用上述查询连接到SQL数据库以获取表中的所有数据。
SqlDataAdapter myCommand = new SqlDataAdapter(selectQuery,conn);
//创建并填充一个DataSet。
DataSet ds = new DataSet();
myCommand.Fill(ds);
以及如何借助以下代码将它们保存在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(toExcel.Columns中的DataColumn列)
{
context.Response.Write(column.ColumnName +,);
}
context.Response.Write(Environment.NewLine);
foreach(在toExcel.Rows中的DataRow行)
{
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();
$ / code>
现在我的问题是如何转换这个 DataSet
到 DataTable
?我已经尝试过 NO 运气: http://www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspx
任何人都可以帮助我吗?
DataTables
。您可以使用: DataTable firstTable = dataSet.Tables [0];
或名称:
DataTable customerTable = dataSet.Tables [Customer];
请注意,您应该使用语句为 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. So far I have figured out how to retrieve the data and fill them in a dataset for display purpose (which is working perfectly)... and how to save them in a CSV file with the help of following code from: http://www.evontech.com/login/topic/1983.html Now my problem is how do I convert this Can anyone help me? A or by name: Note that you should have
这篇关于如何将DataSet转换为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!你的SQL代码,以确保连接正确处置:
$ p $ 使用(SqlConnection conn = ...)
{
//此处的代码...
}
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);
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();
}
DataSet
to DataTable
? I have tried the way described here with NO luck: http://www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspxDataSet
already contains DataTables
. You can just use:DataTable firstTable = dataSet.Tables[0];
DataTable customerTable = dataSet.Tables["Customer"];
using
statements for your SQL code, to ensure the connection is disposed properly:using (SqlConnection conn = ...)
{
// Code here...
}