如何将数据从CSV文件写入SQL表?

时间:2015-03-30 14:37:16

标签: c# sql sql-server sorting

我正在编写 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 文本文件中捕获这些数据,然后将其排序到一个表格,看起来像 tblStudents tblPaid < /强>

对于 tblStudents 我必须把所有代码&#34; ab11&#34;并写入表格。

对于 tblPaid 我只需要编写没有&#34; ab11 &#34;的代码的数据。并放入一个字段,用于读取 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,但无法在我的表格中找到任何内容

任何帮助都将受到赞赏,因为我是开发中的初学者

2 个答案:

答案 0 :(得分: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
    }
  }

答案 1 :(得分:0)

我正在查看我的旧问题,只是看到我从未给你反馈。

我使用 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); } }

运行此操作

在我的申请中这样做的部分写成如下:

http://

感谢您的建议。