1

我想将多个表记录导出到 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());
        }

请帮助我如何使用线程处理多个表数据

4

1 回答 1

2

在这里,您将找到 c# 中的线程教程。基本上你需要使用你的导出方法作为线程方法。为您将创建的所有线程保留处理程序是很好的。

更具体地说......步骤1)为出口商创建类

public class CSVExport
{
    // This method that will be called when the thread is started
    public void exportToCSVfile(object fileOut)
    {
        ...
    }
};

响应用户选择要导出的表,您运行以下

CSVExport obj = new CSVExport();
Thread t = new Thread (CSVExport.exportToCSVfile);
t.Start(oFileName);

您必须确保文件名是唯一的,正如我之前所说,最好将所有线程处理程序保存在一个列表中。

于 2013-01-03T12:05:57.273 回答