Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
MySqlBulkLoader bl = new MySqlBulkLoader(conn);
          bl.TableName = "blancco";
          bl.FieldTerminator = ",";
          bl.LineTerminator = "\n";
          bl.FileName = file;

          try
          {
              Console.WriteLine("Connecting to MySQL...");
              conn.Open();

              // Upload data from file
              int count = bl.Load();
              Console.WriteLine(count + " lines uploaded.");

              conn.Close();
              Console.ReadLine();
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.ToString());
              Console.ReadLine();
          }
          Console.WriteLine("Done.");
          Console.ReadLine();
      }



Hello,

I have a problem with inserting data from csv file to mysql,the problem is that the date is enclosed in double quotation and when I remove quotation all gets miss aligned any sugestions please as I am struggling for couple days now.

Thank you
Posted
Comments
Nathan Minier 13-Apr-15 8:05am    
Your code snipped doesn't show the MySqlBulkLoader.Load(), which is needed at a minimum to see how you're parsing the csv.
Sinisa Hajnal 14-Apr-15 3:29am    
Why does it get misaligned? The date does not contain comma it shouldn't affect field ordering.
BulletVictim 14-Apr-15 4:41am    
Try ading the csv data to a DataTable in your code and the BulkCopy the datatadble into SQL
SyAndroidDog 15-Apr-15 7:38am    
solved use load infile etc

1 solution

C#
using (CsvDataReader csvData = new CsvDataReader(path, ',', Encoding.UTF8))
    {
        // will read in first record as a header row and
        // name columns based on the values in the header row
        csvData.Settings.HasHeaders = true;

        csvData.Columns.Add("nvarchar");
        csvData.Columns.Add("float"); // etc.

        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
        {
            bulkCopy.DestinationTableName = "DestinationTable";
            bulkCopy.BulkCopyTimeout = 3600;

            // Optionally, you can declare columnmappings using the bulkCopy.ColumnMappings property

            bulkCopy.WriteToServer(csvData);
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900