我想将多个表记录导出到 c#.net 中的单个 CSV 文件。
说
一个包含 10 个表的列表框。如果我单击表 1,则该表开始导出到 csv。同时,我将单击列表框中的另一个表,例如表 2,然后应该为该表 2 开始导出到 csv。有点多线程。下面是我用来导出到 csv 的代码,但它仅适用于一张表
public void exportToCSVfile(string fileOut)
{
// Connects to the database, and makes the select command.
OracleConnection conn = new OracleConnection("Data Source=" + DataSource + ";User Id=" + UserId + ";Password=" + Password);
string sqlQuery = "SELECT * FROM " + this.lbxTables.SelectedItem.ToString().ToUpper().Trim();
OracleCommand command = new OracleCommand(sqlQuery, conn);
conn.Open();
// Creates a SqlDataReader instance to read data from the table.
using (OracleDataReader dr = command.ExecuteReader())
{
// Retrives the schema of the table.
DataTable dtSchema = dr.GetSchemaTable();
// Creates the CSV file as a stream, using the given encoding.
using (StreamWriter sw = new StreamWriter(fileOut, false, this.encodingCSV))
{
string strRow; // represents a full row
// Writes the column headers if the user previously asked that.
if (this.chkFirstRowColumnNames.Checked)
{
sw.WriteLine(columnNames(dtSchema, this.separator));
}
// Reads the rows one by one from the SqlDataReader
// transfers them to a string with the given separator character and
// writes it to the file.
MessageBox.Show("Export to CSV has started for the Table: " + this.lbxTables.SelectedItem.ToString().ToUpper().Trim());
while (dr.Read())
{
strRow = "";
for (int i = 0; i < dr.FieldCount; i++)
{
strRow += Convert.ToString(dr.GetValue(i)) + separator;
}
if (separator.Length > 0)
strRow = strRow.Substring(0, strRow.LastIndexOf(separator));
sw.WriteLine(strRow);
}
}
dr.Close();
dr.Dispose();
}
// Closes the text stream and the database connenction.
conn.Close();
//// Notifies the user.
MessageBox.Show("Export to CSV has completed for the Table: " + this.lbxTables.SelectedItem.ToString());
}
请帮助我如何使用线程处理多个表数据