简体   繁体   English

从数据库表写入文本文件的最快方法是什么?

[英]What is the fastest way to write to a text file from a database table?

I have data analysis application and I need to be able to export database tables to a delimited text file using c#. 我有数据分析应用程序,我需要能够使用c#将数据库表导出到分隔的文本文件。 Because of the application architecture, that data must be brought to the c# application. 由于应用程序体系结构,必须将该数据引入c#应用程序。 No database exporting functionality can be used. 不能使用数据库导出功能。 The tables size can range from a few columns and a few hundred rows to ~100 columns to over a million rows. 表的大小可以从几列到几百行到~100列到超过一百万行。

Further clarification based on comments -- 根据意见进一步澄清 -

I have a Windows Service acting as the data access layer that will be getting the request for the export from the presentation layer. 我有一个Windows服务作为数据访问层,将从表示层获取导出请求。 Once the export is complete, the service will then need to pass the export back to the presentation layer, which would either be a WPF app or a Silverlight app, as a stream object. 导出完成后,服务将需要将导出作为流对象传递回表示层(可以是WPF应用程序或Silverlight应用程序)。 The user will then be given an option to save or open the export. 然后,将为用户提供保存或打开导出的选项。

What is the fastest way to do this? 最快的方法是什么?

Thanks 谢谢

hmm, first of all, if its not a must to use c#, the sql managment console is capable of such a task. 嗯,首先,如果它不是必须使用c#,则sql管理控制台能够完成这样的任务。

To achieve best perfrormance i would you a consumer-producer 2 thread concept, 为了实现最佳的性能,我会为消费者 - 生产者2线程概念,

  • One thread will be the reader, responsible for reading items from the DB - in which case i highly recommand using the IReader to read the values, and put them in a cuncurrent queue. 一个线程将是读者,负责从数据库中读取项目 - 在这种情况下,我强烈建议使用IReader读取值,并将它们放在一个cuncurrent队列中。
  • The other will be the writer who will simply use a fileStream to write the data from the queue. 另一个是将只使用fileStream从队列中写入数据的编写者。

you can also achieve much greater performance by reading the information via a paged manner, thats is, if you know you'll have 100000 records, devide it to chunks of 1000, have a reader reading those chunks from the DB and putting them in a queue. 你也可以通过分页方式阅读信息来获得更高的性能,也就是说,如果你知道你将拥有100000条记录,将其分成1000块,让读者从数据库中读取这些块并将它们放入队列。

Although the later solution is more complicated he'll allow you to utilize your CPU in the best way possibble and avoid latency. 虽然后面的解决方案更复杂,但它允许您以最佳方式利用CPU并避免延迟。

If you are using SQL Server 2008 (or maybe 2005), you can right-click the database and choose "Tasks->Export Data". 如果您使用的是SQL Server 2008(或可能是2005),则可以右键单击数据库并选择“任务 - >导出数据”。 Choose your database as input, and choose the "Flat file destination" as output. 选择数据库作为输入,并选择“平面文件目标”作为输出。 Specify the file name, specify double-quote as the text qualifier, click "next" a few times and you're done. 指定文件名,指定双引号作为文本限定符,单击“下一步”几次,然后就完成了。 You can even save the task as an SSIS package that you can run again. 您甚至可以将任务保存为可以再次运行的SSIS包。

Doing it this way uses SSIS under the covers. 这样做是在封面下使用SSIS。 It has very high performance, as it uses multiple threads in a pipeline. 它具有非常高的性能,因为它在管道中使用多个线程。

我会看一下使用SQLBulkCopy对象。

If you really need to use C#, the fastest way would be to use ADO.NET's DataReader, it is read-only and forward-only, may suit you well. 如果你真的需要使用C#,最快的方法是使用ADO.NET的DataReader,它是只读的,只有前向的,可能适合你。 Just be careful with null fields, it doesn't handle very well, if you need to deal with them, maybe other ADO.NET resources will be more interesting for you. 只要注意空字段,它处理得不好,如果你需要处理它们,也许其他ADO.NET资源对你来说会更有趣。

如果您需要快速查询数据,可以在一个或多个线程中使用“Firehose”游标,直接从数据库中读取。

var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
var sqlDataAdapter = new SqlDataAdapter("select * from tnm_story_status", sqlConnection); sqlConnection.Open();
var dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet); sqlConnection.Close();
var dataTable = dataSet.Tables[0];
var streamWriter = new StreamWriter(@"C:\\db.txt", false);
var sb = new StringBuilder();
for (var col = 0; col < dataTable.Columns.Count; col++)
{
if (sb.ToString() != "") sb.Append(",");
sb.Append(dataTable.Columns[col].ColumnName);
}
streamWriter.WriteLine(sb.ToString());
sb.Remove(0, sb.ToString().Length);
for (var row = 0; row < dataTable.Rows.Count; row++ )
{
for (var col = 0; col < dataTable.Columns.Count; col++)
{
if (sb.ToString() != "") sb.Append(",");
sb.Append(dataTable.Rows[row][col].ToString());
}
streamWriter.WriteLine(sb.ToString());
sb.Remove(0, sb.ToString().Length);
}
streamWriter.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文本文件加载到Access数据库的最快方法是什么? - What is the fastest way to load a text file to an access Database? 从文本文件加载数据并将其存储到数据库的最快方法 - Fastest way to load data from text file then store it into database 将文本文件导入数据库的最快方法 - Fastest way to import text file into database 将文本文件加载到RichTextBox的最快方法是什么? - What is the fastest way to load text file into RichTextBox? 使用C#写入Excel文件的最佳和最快的方法是什么? - What is the best and fastest way to write into Excel file using C#? 从SQLite数据库文件检索一百万条记录并将其显示在WPF Datagrid中的最快方法是什么? - What is the fastest way to retrieve a million records from an SQLite database file and display it in the WPF Datagrid? 将从文本文件解析的250万行数据插入Sql Server的最快方法是什么 - What would be the fastest way to insert 2.5 million rows of data parsed from a text file, into Sql server 逐行读取文本文件的最快方法是什么? - What's the fastest way to read a text file line-by-line? 从数据表填充SQLite表的最快方法是什么 - what is the fastest way to populate SQLite table from a datatable 在文件中查找文本的最快方法 - Fastest way to find text in file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM