【问题标题】:How to write data from CSV file to SQL table?如何将数据从 CSV 文件写入 SQL 表?
【发布时间】:2015-06-03 15:18:31
【问题描述】:

我正在编写一个 C# windows 应用程序,但我似乎不知道如何将数据从 CSV 文件插入到数据库中。

我有一个名为“data.csv”的 csv 文件,其记录如下

Header 201501
id      code    amount
1       ab11    5000
2       ab11    6000
3       ab11    8000
3       wx34    2500
3       df21    1000
4       ab11    7000
4       zx54    3500

我必须将这些数据放到 sql 表中,如下所示

ab11 表示要支付的费用 任何其他代码代表学生已支付的金额

第一个表 tblStudents

id      code    amount
1       ab11    5000
2       ab11    6000
3       ab11    8000
4       ab11    7000
5       ab11    9000

第二张表 tblPaid

id     code  Paid  Month
3       wx34    2500  201401
3       df21    1000  201401
4       zx54    3500  201401

基本上我要做的是从 data.csv 文本文件中捕获这些数据并将其排序到一个表格中,使其看起来像 tblStudentstblPaid。

对于 tblStudents,我必须将代码“ab11”的所有内容写入表格。

对于 tblPaid,我只需要只写没有“ab11”代码的数据,并放入一个读取日期旁边的字段data.csv 文本文件的标题。

我正在考虑使用 BULK INSERT,但标题不应该在表格中。

所以我只能创建表格并读取 data.csv 文本文件,如下所示

        //Open File Dialog to open csv file
        //Only get csv files
        openFileDialog1.Filter = ".csv file|*.csv*";
        openFileDialog1.FilterIndex = 1;

        DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.Yes)
        {
            String file = openFileDialog1.FileName;

            //Read csv text file to get the month in the first line
            using (var reader = new StreamReader(file))
            {
                string firstLine = reader.ReadLine();
                string[] splitedLine = firstLine.Split(';');
                string month = splitedLine[1];
            }
            try
            {
                //String with connection information of the database PSAHoldings
                string connect = "Data Source=BRIAN-PC\\SQLEXPRESS; Initial Catalog=PSAHoldings; user id =sa; Password=kagiso";

                //String Query to create t_original table if it does not exist already
                string table = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_original' AND xtype='U')" +
                    "CREATE TABLE t_original (" +
                    "empId varChar(10) NULL," +
                    "paycode varChar(10) NULL," +
                    "amount int NULL," +
                    ")";

                //String Query to create tblStudents table if it does not exist already
                string tblStudents = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_employee' AND xtype='U')" +
                    "CREATE TABLE t_employee (" +
                    "empId varChar(10) NOT NULL PRIMARY KEY," +
                    "paycode varChar(10) NULL," +
                    "amount int NULL," +
                    ")";

                //String Query to create tblPaid table if it does not exist already
                string Paid = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_deduction' AND xtype='U')" +
                    "CREATE TABLE t_deduction (" +
                    "empId varChar(10) NOT NULL PRIMARY KEY," +
                    "amount int NULL," +
                    "balance int NULL," +
                    ")";

                //String Query to create t_institutions table if it does not exist already
                string t_institutions = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='t_institutions' AND xtype='U')" +
                    "CREATE TABLE t_institutions (" +
                    "empId varChar(10) NOT NULL PRIMARY KEY," +
                    "paycode varChar(10) NOT NULL," +
                    "amount int NULL," +
                    ")";


                //Connecting to the server/database
                SqlConnection con = new SqlConnection(connect);
                con.Open();

                //Create the tables
                SqlCommand createTable = new SqlCommand(table, con);
                SqlCommand createEmployee = new SqlCommand(t_employee, con);
                SqlCommand createDeductions = new SqlCommand(t_deduction, con);
                SqlCommand createInstitution = new SqlCommand(t_institutions, con);

                createTable.ExecuteNonQuery();
                createEmployee.ExecuteNonQuery();
                createDeductions.ExecuteNonQuery();
                createInstitution.ExecuteNonQuery();


                //String Query to insert the text file into the t_original table
                String BulkInsert = "BULK INSERT t_original FROM_" +
                    file + "_WITH (--FIRSTROW = 3," +
                    "FIELDTERMINATOR = ''," +
                    "MAXERRORS = 0," +
                    "ROWTERMINATOR = '\\n')";
                //"UPDATE t_original"+
                //"SET month =" + month + 
                //"WHERE month is null";

                //insert the text file into the t_original table
                SqlCommand bulkCmd = new SqlCommand(BulkInsert, con);
                bulkCmd.ExecuteNonQuery();


            }
            //Catch exeption
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString(), "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

我已经写了一个BULK_INSERT,但我的表中没有任何东西

任何帮助都将不胜感激,因为我是开发初学者

【问题讨论】:

  • 另外,BULK INSERT 有 FIRST_ROW= 子句允许它跳过文件开头的一些行。
  • 为什么第二个表中有'ab11'的记录?
  • @Steve 抱歉,我已经纠正了我的打字错误。第二个表不应该有“ab11”
  • @RBarryYoung 谢谢你,我不知道如何跳过第一行。因此,既然我假设 FIRST_ROW 子句仅适用于第一行而不适用于第二行,那么是否可以跳到行?

标签: c# sql sql-server sorting


【解决方案1】:

首先,您只需在数据库中创建表一次。因此,将创建它们的整个代码块移出循环。 其次,对于从 csv 读取的每一行,您需要确定它是属于学生表还是付费表。大致如下:

string[] DataArray = line.split(',');
  if(DataArray.Length==3) // this is an actual data line
  {
    if(DataArray[1] == "ab11") 
    {
    // student row
    } 
    else 
    {
     // Paid row
    }
  }

【讨论】:

  • 我会试试看能不能这样得到输出
【解决方案2】:

我正在处理我的旧问题,只是发现我从未给过你反馈。

我使用 SQLBULKCOPY 运行它

在我的应用程序中执行此操作的部分如下所示:

    public override bool FileToDatabase(string filePath)
    {
        SqlConnection sqlConnection = new SqlConnection(“Connerction string”);

        try
        {
            StreamReader readFile = new StreamReader(filePath);

            string[] sArr = ("LineNumber|" + readVoteFile.ReadLine()).Split('|');

            int columnCount = 0;
            foreach (string s in sArr)
            {
                columnCount++;                    
            }                

            string line = null;
            long counter = 0;
            bool hasMoreRows = true;
            DataTable FileDataTable = null;

            while (hasMoreRows)
            {

                line = readFile.ReadLine();

                if (line == null)
                {
                    hasMoreRows = false;
                }
                else
                {

                    if (hasMoreRows && FileDataTable == null)
                    {
                        FileDataTable = new DataTable();
                        for (int i = 0; i < columnCount; i++)
                        {
                            FileDataTable.Columns.Add(new DataColumn());
                        }
                    }

                    DataRow row = FileDataTable.NewRow();
                    row.ItemArray = (counter + "|" + line).Split('|');
                    FileDataTable.Rows.Add(row);
                }


                if (counter != 0 && counter % base.FileProcessBatchSize == 0 && hasMoreRows || !hasMoreRows && FileDataTable != null && FileDataTable.Rows.Count > 0)
                {
                    sqlConnection.Open();

                    SqlBulkCopy bc = new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.TableLock, null);
                    bc.BatchSize = base.DBInsertBatchSize;
                    bc.BulkCopyTimeout = 1200;
                    bc.DestinationTableName = base.BulkCopyTable;
                    bc.WriteToServer(FileDataTable);
                    bc.Close();

                    sqlConnection.Close();

                    FileDataTable = null;
                }

                counter++;
            }

            readFile.Close();
            readFile.Dispose();

            return true;
        }
        catch(Exception ex)
        {
            try { sqlConnection.Close(); }
            catch { }

            throw (ex);
        }
    }

感谢您的建议。

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多