Carriage返回打破MySQL BulkLoader

时间:2016-01-11 11:09:14

标签: c# mysql excel csv

我将Visual FoxPro中的数据导入MySQL,但我在阅读Memo列类型时遇到问题。问题是在FoxPro的Memo列中,用户可以输入回车。

我将FoxPro表加载到CSV文件中,并且出现问题,回车被计为新行,这会在我使用MySqlBulkLoader时混淆导入过程。 CSV文件如下所示;

Contact1
Contact2
description associated with Contact2
description associated with Contact2
Contact3

这里的问题是Contact2的描述中有回车。有没有办法我可以遍历CSV文件,以便它可以忽略回车,但在MySQL数据库中维护它们?我是否可以遍历C#DataTable并将其删除?因此,不要因为回车而把它们保持在事物的MySQL方面而感到困惑。

这些是我目前正在使用的方法;

    private void Contacts()     
    {
        using (OleDbConnection dbfCon = new OleDbConnection(dbfConstr))
        {
            dbfCon.Open();
            var dbfCmd = new OleDbCommand(@"SELECT ct_id, ct_cmpid, ct_empid,
            ct_pplid, ct_cntid, ct_pplnm, ct_date, ct_time, ct_type, ct_doneby
            FROM contacts", dbfCon);
            var dTable = new DataTable();
            var dataAdapter = new OleDbDataAdapter(dbfCmd);
            dataAdapter.Fill(dTable);
            WriteDateTimeToCSV(dTable, "contacts");
        }
    }

将数据从CSV加载到MySQL

    public void WriteDateTimeToCSV(DataTable dataTable, string mySqlTable)
    {
        var sb = new StringBuilder();
        IEnumerable<string> columnNames = dataTable.Columns.Cast<DataColumn>().
                                          Select(column => column.ColumnName);
        sb.AppendLine(string.Join(",", columnNames));

        Type _datetype = typeof(DateTime);

        foreach (DataRow row in dataTable.Rows)
        {
            IEnumerable<string> fields = row.ItemArray.Select((field, index) => (dataTable.Columns[index].DataType == _datetype) ? ((DateTime)field).ToString("yyyy-MM-dd") :
            (field.ToString().Contains(",") ? "\"" + field.ToString() + "\"" : field.ToString()));
            sb.AppendLine(string.Join(",", fields));

        }
        File.WriteAllText(@"C:\Users\DanD\Desktop\test.csv", sb.ToString());
        WriteToMySQL(mySqlTable);
    }

WriteToMySQL

    public void WriteToMySQL(string mySqlTable)
    {
        MySqlConnection mysqlCon = new MySqlConnection(mysqlConstr);
        mysqlCon.Open();
        string tempCsvFileSpec = @"C:\Users\DanD\Desktop\test.csv";
        var msbl = new MySqlBulkLoader(mysqlCon);
        msbl.TableName = "sdcdatabase." + mySqlTable;
        msbl.NumberOfLinesToSkip = 1;
        msbl.FileName = tempCsvFileSpec;
        msbl.FieldTerminator = ",";
        msbl.FieldQuotationCharacter = '"';
        msbl.Load();
        File.Delete(tempCsvFileSpec);
    }

编辑:这是在Notepad ++中打开的CSV

32,2199,9,26,0,Dan                              ,1996-08-13,16:56:12  ,4,Dan    ,"OTT 30/08/96. CRLF
    Test 1 CRLF
    Test 2 CRLF
    Test 3 CRLF
    33,3146,9,512,0,Dan                          ,1996-08-20,10:51:14  ,1,Dan    ,Call again re Malvern CRLF

到目前为止,我已经尝试过替换CR;

        if (row[10].ToString().Contains("CR"))
        {
            row[10].ToString().Replace("CR", "!");
        }

但不幸的是CR没有被取代

0 个答案:

没有答案