C# 使用线程将多个表数据导出到c.net中的csv

C# 使用线程将多个表数据导出到c.net中的csv,c#,export-to-csv,C#,Export To Csv,我想将多个表记录导出到c.net中的单个CSV文件中 说 包含10个表的列表框。如果单击表1,则该表将开始导出到csv。同时,我将单击列表框中的另一个表,即表2,然后该表2应开始导出为csv。有点多线程。下面是我用来导出到csv的代码,但它只用于一个表 public void exportToCSVfile(string fileOut) { // Connects to the database, and makes the select command

我想将多个表记录导出到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());
        }
请帮助我如何使用线程处理多表数据

您将在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);

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

我不羡慕您,祝您好运!注:你不需要关闭或处理,因为你有一个正常使用的,这是使用的好处