快速的CSV文件生成器
生活随笔
收集整理的這篇文章主要介紹了
快速的CSV文件生成器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹
在某些應用程序中,往往需要將數據庫表中的數據取出來存為CSV文件。看起來是很容易的一件事情,但是如果要做到靈活,而且要在取大數據量的表時保證足夠的性能,卻需要認真考慮。本CSV文件生成器的設計考慮滿足以下指標:
1、支持多種不同的數據庫,如Oracle,SQL Server等
2、程序尺寸要小
3、足夠的性能 程序的設計
基本的思路是采用C++通過ATL作出一個COM組件,然后用解釋性語言如C#來調用。數據庫訪問采用ADO來實現,從本人的經驗來說,我是不太愿意使用ADO的,雖然它很通用各種數據庫都可以訪問,微軟也很推崇,但是它對多線程應用支持的卻很差,而且一堆繁瑣的智能指針,能把人給扎死。但是考慮到其通用性,而且現在好像比較時髦,還是勉為其難的采用了ADO. CSV文件生成器說明
1. 支持不同數據庫訪問
2. 滿足CSV文件格式的要求
3. 能支持指定多個表的CSV文件生成
4. 支持SQL語句的CSV文件生成
5. 可設置文件后綴、分隔符等
方法描述
屬性描述
? ? ? ? ? ? ? ? ? ? ? 怎樣實驗CSV文件生成器
在提供的可執行文件包中包含了兩個文件一個是ATLCSV.dll,一個是ATLV6Client.exe, 先用如下命令要注冊一下組件
D:\> regsvr32 /c atlcsv.dll
然后敲一下atlv6client,看一下程序的使用說明,
D:\>atlv6client
Usage:
??????? atlv6client user=... table=... sql=... field=... row=... suffix=...
(@) Copyright Thomas WANG 2007, all rights reserved.
Parameters:
??????? user=username/password@tnsname.mandatory parameter
??????? table=table1|table2|...|tablen.mandatory parameter if not specify sql
??????? sql=SQL statement.mandatory parameter if not specify table
??????? opt=nMode|nStart|nDelta start position and Delta.optional.nMode can
???????? be 1,2or3. 1:default value 2:segmentation operations on a table
??????? 3:fetch to cache and write.if say 2, also speficy nStart,nDelta.
??????? field=seperator string between fields. default is ","
??????? row=seperator string between rows. default is "\n"
??????? suffix=file suffix. default is csv
??????? Notes: No space adjacent to = 這個程序是一個VC++ 6.0寫的一個控制臺程序,在正式運行前保證你已經有了一個數據源。例如我機器上有一個ODBC數據源TEST,我想取數據庫中TAB這個表,可用如下命令:
D:\> atlv6client user=system/Helpdesk1@test table="tab"
Parsing DB info
Connect to DB ...
Write CSV file spent: 0:2
Fetch Data and Write ...
Write CSV file spent: 0:1
Finished
在目錄下,會生成一個以表名命名的CSV文件。以上這種指定表主要用于在有很多表需要導出的場合或需要許多控制的時候使用。也可以用如下命令完成以上的功能
D:\>atlv6client user=system/Helpdesk1@test sql="select * from tab"
Parsing DB info
Connect to DB ...
Write CSV file spent: 0:0
Fetch DB ...
Write select * from tab to file
Write CSV file spent: 0:0
Finished 當你在導出一個大表時,如這個表有一百萬條記錄,你可以用以下命令:
D:\> atlv6client user=system/Helpdesk1@test table="vehicle" opt="2|1|50000"
Parsing DB info
Connect to DB ...
Write CSV file spent: 0:1
Fetch Data and Write ...
讓它以5萬條步長處理數據。這樣機器的CPU內存占用也不會那么多。 如何編寫C#客戶端 以上提供的客戶端是C++寫的,如果你想在C#里調用那就太方便不過了,首先,在增加引用菜單里加入COM組件
然后增加如下代碼即可 ? private void Form1_Load(object sender, EventArgs e)
??????? {
??????????? ATLCSVLib.ComCsvSrvClass bb = new ComCsvSrvClass();
??????????? bb.ConnectionString = "dsn=test;user=system;pwd=Helpdesk1;";
??????????? bb.NumOfTables = 2; ?????????? bb.set_bstrTableName(0, "vehicle");
??????????? bb.set_bstrTableName(1, "van");
??????????? bb.OnConnectDbOk += new _IComCsvSrvEvents_OnConnectDbOkEventHandler(bb_OnConnectDbOk);
??????????? bb.OnDataFechFinished += new _IComCsvSrvEvents_OnDataFechFinishedEventHandler(bb_OnDataFechFinished);
??????????? bb.OnWriteCSV += new _IComCsvSrvEvents_OnWriteCSVEventHandler(bb_OnWriteCSV);
??????????? bb.BlindFetchCSVData();???????????
??????? } ??????? void bb_OnWriteCSV(string bstrFileInfo)
??????? {
??????????? MessageBox.Show("Write!");???????????
??????? } ??????? void bb_OnDataFechFinished(string theString)
??????? {
??????????? MessageBox.Show("Fetch!");???????????
??????? } ??????? void bb_OnConnectDbOk(string strDBInfo)
??????? {
??????????? MessageBox.Show("Connection!");???????????
??????? } 考慮到目前CSV組件我仍在改進中,所以只傳了可行文件,和客戶端程序,如果你要組件源碼,發郵件到[email]thomas.x.wang@gmail.com[/email].
在某些應用程序中,往往需要將數據庫表中的數據取出來存為CSV文件。看起來是很容易的一件事情,但是如果要做到靈活,而且要在取大數據量的表時保證足夠的性能,卻需要認真考慮。本CSV文件生成器的設計考慮滿足以下指標:
1、支持多種不同的數據庫,如Oracle,SQL Server等
2、程序尺寸要小
3、足夠的性能 程序的設計
基本的思路是采用C++通過ATL作出一個COM組件,然后用解釋性語言如C#來調用。數據庫訪問采用ADO來實現,從本人的經驗來說,我是不太愿意使用ADO的,雖然它很通用各種數據庫都可以訪問,微軟也很推崇,但是它對多線程應用支持的卻很差,而且一堆繁瑣的智能指針,能把人給扎死。但是考慮到其通用性,而且現在好像比較時髦,還是勉為其難的采用了ADO. CSV文件生成器說明
1. 支持不同數據庫訪問
2. 滿足CSV文件格式的要求
3. 能支持指定多個表的CSV文件生成
4. 支持SQL語句的CSV文件生成
5. 可設置文件后綴、分隔符等
方法描述
| Methods<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> | Description |
| BlindFetchCSVData | Fetch data from one or multiple tables and writes to CSV files. File name has same name as table name |
| FetchCsvData | Fetch data into cache specified by a SQL |
| WriteToCSV | Write Cache Data into a CSV file. |
| GetConnectionStringFromCfg | Get DB connection info from a profile file |
| FetchAndWriteCsv | Fetch data specified by a SQL and write to CSV file |
| Properties | Description |
| ConnectionString | DB connection string. |
| FileSuffix | File suffix.default is csv. |
| FieldsDelimiter | Delimiter between fields. |
| RecordsDelimiter | Delimiter between rows. |
| Header | Specify header is required or not. default is required. |
| NumOfTables | Number of tables to be queried |
| TableName | Table name to be queried |
在提供的可執行文件包中包含了兩個文件一個是ATLCSV.dll,一個是ATLV6Client.exe, 先用如下命令要注冊一下組件
D:\> regsvr32 /c atlcsv.dll
然后敲一下atlv6client,看一下程序的使用說明,
D:\>atlv6client
Usage:
??????? atlv6client user=... table=... sql=... field=... row=... suffix=...
(@) Copyright Thomas WANG 2007, all rights reserved.
Parameters:
??????? user=username/password@tnsname.mandatory parameter
??????? table=table1|table2|...|tablen.mandatory parameter if not specify sql
??????? sql=SQL statement.mandatory parameter if not specify table
??????? opt=nMode|nStart|nDelta start position and Delta.optional.nMode can
???????? be 1,2or3. 1:default value 2:segmentation operations on a table
??????? 3:fetch to cache and write.if say 2, also speficy nStart,nDelta.
??????? field=seperator string between fields. default is ","
??????? row=seperator string between rows. default is "\n"
??????? suffix=file suffix. default is csv
??????? Notes: No space adjacent to = 這個程序是一個VC++ 6.0寫的一個控制臺程序,在正式運行前保證你已經有了一個數據源。例如我機器上有一個ODBC數據源TEST,我想取數據庫中TAB這個表,可用如下命令:
D:\> atlv6client user=system/Helpdesk1@test table="tab"
Parsing DB info
Connect to DB ...
Write CSV file spent: 0:2
Fetch Data and Write ...
Write CSV file spent: 0:1
Finished
在目錄下,會生成一個以表名命名的CSV文件。以上這種指定表主要用于在有很多表需要導出的場合或需要許多控制的時候使用。也可以用如下命令完成以上的功能
D:\>atlv6client user=system/Helpdesk1@test sql="select * from tab"
Parsing DB info
Connect to DB ...
Write CSV file spent: 0:0
Fetch DB ...
Write select * from tab to file
Write CSV file spent: 0:0
Finished 當你在導出一個大表時,如這個表有一百萬條記錄,你可以用以下命令:
D:\> atlv6client user=system/Helpdesk1@test table="vehicle" opt="2|1|50000"
Parsing DB info
Connect to DB ...
Write CSV file spent: 0:1
Fetch Data and Write ...
讓它以5萬條步長處理數據。這樣機器的CPU內存占用也不會那么多。 如何編寫C#客戶端 以上提供的客戶端是C++寫的,如果你想在C#里調用那就太方便不過了,首先,在增加引用菜單里加入COM組件
然后增加如下代碼即可 ? private void Form1_Load(object sender, EventArgs e)
??????? {
??????????? ATLCSVLib.ComCsvSrvClass bb = new ComCsvSrvClass();
??????????? bb.ConnectionString = "dsn=test;user=system;pwd=Helpdesk1;";
??????????? bb.NumOfTables = 2; ?????????? bb.set_bstrTableName(0, "vehicle");
??????????? bb.set_bstrTableName(1, "van");
??????????? bb.OnConnectDbOk += new _IComCsvSrvEvents_OnConnectDbOkEventHandler(bb_OnConnectDbOk);
??????????? bb.OnDataFechFinished += new _IComCsvSrvEvents_OnDataFechFinishedEventHandler(bb_OnDataFechFinished);
??????????? bb.OnWriteCSV += new _IComCsvSrvEvents_OnWriteCSVEventHandler(bb_OnWriteCSV);
??????????? bb.BlindFetchCSVData();???????????
??????? } ??????? void bb_OnWriteCSV(string bstrFileInfo)
??????? {
??????????? MessageBox.Show("Write!");???????????
??????? } ??????? void bb_OnDataFechFinished(string theString)
??????? {
??????????? MessageBox.Show("Fetch!");???????????
??????? } ??????? void bb_OnConnectDbOk(string strDBInfo)
??????? {
??????????? MessageBox.Show("Connection!");???????????
??????? } 考慮到目前CSV組件我仍在改進中,所以只傳了可行文件,和客戶端程序,如果你要組件源碼,發郵件到[email]thomas.x.wang@gmail.com[/email].
轉載于:https://blog.51cto.com/thomas/29358
總結
以上是生活随笔為你收集整理的快速的CSV文件生成器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看图填成语,猜古诗,看你知道几个!
- 下一篇: Win10系统gpedit.msc文件找