简体   繁体   中英

C# Importing .CSV Data Into MySQL Table Only Copies 1 Column

I have a method written in C# that is attempting to copy the data in a .CSV file into a MySQL table that looks like this;

     public void writeToMySQL()
     {
        string constr = ConfigurationManager.ConnectionStrings["sqlString"].ConnectionString;
        using (MySqlConnection sqlCon = new MySqlConnection(constr))
        {
            try
            {
                sqlCon.Open();
                MySqlCommand sqlCmd = new MySqlCommand("LOAD DATA INFILE 'test.csv' INTO TABLE sdcdbftest.importexport;", sqlCon);
                sqlCmd.ExecuteNonQuery();
            }
            catch (MySqlException)
            {
                throw;
            }
        }
    } 

What is happening however is that only the data from the first column of the .CSV file is copied over, nothing else. The .CSV file looks like so;

在此输入图像描述

Finally this is how the table is structured on the Database through phpMyAdmin;

在此输入图像描述

Is there any obvious reason as to why only the first column (cs_id) is being copied over?

From the documentation , The default column terminator for the LOAD DATA INFILE... command is the tab ( \\t ) character. So you need to specify you are using commas in the command like this:

LOAD DATA INFILE 'test.csv' 
    INTO TABLE sdcdbftest.importexport 
    FIELDS TERMINATED BY ',';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM