Select 多个 CSV 和批量到数据库

Select Multiple CSV and bulk to Database

我使用带有 OpenFileDialog class 的 MDF 数据库将单个 CSV 文件导入数据库。这适用于单个 CSV 文件,但我需要批量打开和处理多个 CSV 文件。

如何改进我的代码?我试过使用 for 循环。

这是我处理单个 CSV 文件的代码:

ofd.Filter = "CSV files (*.csv) | *.csv; | CSV PRN files (*.prn,) |*.prn;";
ofd.FileName = "";
ofd.ShowDialog();
DataTable dt = new DataTable();
string line = null;
int i = 0;
using (StreamReader sr = File.OpenText(ofd.FileName))
{
    while ((line = sr.ReadLine()) != null)
    {
        string[] data = line.Split(',');
        if (data.Length > 0)
        {
            if (i == 0)
            {
                foreach (var item in data)
                {
                    dt.Columns.Add(new DataColumn());
                }

                i++;
            }

            DataRow row = dt.NewRow();
            row.ItemArray = data;
            dt.Rows.Add(row);
        }

    }
}

string symbolName = dt.Rows[1][0].ToString();
string strConnection =
@"Data Source =.\SQLEXPRESS; AttachDbFilename = C:\USERS\JEF\DOCUMENTS\DATABASE1.MDF; Integrated Security = True; Connect Timeout = 30; User Instance = True";
SqlConnection condb2 = new SqlConnection(strConnection);
string createTablerow ="create table ["+symbolName+"] (code1 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,date1 varchar(50),open1 varchar(50),high1 varchar(50),low1 varchar(50),close1 varchar(50),vol1 varchar(50))";
using (SqlConnection connection = new SqlConnection(strConnection))
{
    SqlCommand command1 = new SqlCommand(createTablerow, connection);
    connection.Open();
    command1.ExecuteNonQuery();

}
using (SqlConnection cn = new SqlConnection(strConnection))
{
    cn.Open();
    using (SqlBulkCopy copy = new SqlBulkCopy(cn))
    {
        copy.ColumnMappings.Add(0, "code1");
        copy.ColumnMappings.Add(1, "date1");
        copy.ColumnMappings.Add(2, "open1");
        copy.ColumnMappings.Add(3, "high1");
        copy.ColumnMappings.Add(4, "low1");
        copy.ColumnMappings.Add(5, "close1");
        copy.ColumnMappings.Add(6, "vol1");
        copy.DestinationTableName = "[" + symbolName + "]";
        copy.WriteToServer(dt);
    }
}

为了一次处理多个文件,您需要使用并行代码。您对 "for" 的使用是一个很好的尝试,但是 "for" 循环仍然 运行 并发,这意味着一次一个。

在这种情况下实现这种并行处理的最简单方法是使用 Parallel.Foreach,这里有一个关于它的 Microsoft 指南:https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop

我移动了你们中的一些代码以提高效率:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ofd.FileName = "";
            ofd.ShowDialog();
            string line = null;
            int i = 0;

            string strConnection =
            @"Data Source =.\SQLEXPRESS; AttachDbFilename = C:\USERS\JEF\DOCUMENTS\DATABASE1.MDF; Integrated Security = True; Connect Timeout = 30; User Instance = True";

            using (SqlConnection connection = new SqlConnection(strConnection))
            {
                connection.Open();
                SqlBulkCopy copy = new SqlBulkCopy(connection);

                copy.ColumnMappings.Add(0, "code1");
                copy.ColumnMappings.Add(1, "date1");
                copy.ColumnMappings.Add(2, "open1");
                copy.ColumnMappings.Add(3, "high1");
                copy.ColumnMappings.Add(4, "low1");
                copy.ColumnMappings.Add(5, "close1");
                copy.ColumnMappings.Add(6, "vol1");


                foreach (string file in ofd.FileNames)
                {
                    using (StreamReader sr = File.OpenText(file))
                    {
                        DataTable dt = new DataTable();

                        while ((line = sr.ReadLine()) != null)
                        {
                            string[] data = line.Split(',');
                            if (data.Length > 0)
                            {
                                if (i == 0)
                                {
                                    foreach (var item in data)
                                    {
                                        dt.Columns.Add(new DataColumn());
                                    }

                                    i++;
                                }

                                DataRow row = dt.NewRow();
                                row.ItemArray = data;
                                dt.Rows.Add(row);
                            }
                        }

                        string symbolName = dt.Rows[1][0].ToString();

                        string createTable = string.Format("create table [{0}] (code1 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,date1 varchar(50),open1 varchar(50),high1 varchar(50),low1 varchar(50),close1 varchar(50),vol1 varchar(50))",
                            symbolName);

                        using (SqlCommand command1 = new SqlCommand(createTable, connection))
                        {
                            command1.ExecuteNonQuery();

                            copy.DestinationTableName = "[" + symbolName + "]";
                            copy.WriteToServer(dt);

                        }
                    }
                }
            }
        }
    }
}