0

I am trying to get a CSV file into a MySQL table, see this thread:

Loading CSV into MySQL Is This a Bug? - File Not Found Exception

I have figured out that executing the command is throwing the exception because of the double backlash in the filename. If I move the CSV file to where the executable is and only supply the filename, it runs just fine. The problem is the LOAD DATA statement only accepts a string for the filename, as per the MySQL docs. I can't pass a parameter and I don't know how in C# to specify '\' in a string without escaping it first. I am using String.Format() to build the command string. I have tried both String.Format(@"") and without the @, neither is working. Does anyone have suggestions? I searched the forum and found an example for PHP, wondering if there is something for C#.

Here's the code I have now, where InputFileListView is a list of files a user has chosen to load into the database. the problem is any of the ToString() methods to access the file path information put the \ in the string. This causes issues with executing the command

IEnumerable<FileInfo> files = this.InputFileListView.Items.Cast<FileInfo>();

 // just using the first entry in InputFileListView for now
string working = String.Format("LOAD DATA LOCAL INFILE '{0}' IGNORE", files.FirstOrDefault().ToString()) +
                         String.Format("INTO TABLE {0} COLUMNS TERMINATED BY ',' LINES TERMINATED BY '\n'", "by_switch")+
                         String.Format("IGNORE 1 LINES (`Switch`,`Port`,`WWPN`,@the_slot,`Port Index`,@the_time,`Interval`,`Port Send Packet Rate`,")+
                         String.Format("`Port Receive Packet Rate`,`Total Port Packet Rate`,`Port Send Data Rate`,")+        
                         String.Format("`Port Receive Data Rate`,`Total Port Data Rate`,`Port Peak Send Data Rate`,`Port Peak Receive Data Rate`,")+ 
                         String.Format("`Port Send Packet Size`,`Port Receive Packet Size`,`Overall Port Packet Size`,`Error Frame Rate`,")+
                         String.Format("`Dumped Frame Rate`,`Link Failure Rate`,`Loss of Sync Rate`,`Loss of Signal Rate`,`CRC Error Rate`,")+
                         String.Format(" `Short Frame Rate`,`Long Frame Rate`,`Encoding Disparity Error Rate`,")+         
                         String.Format("`Discarded Class3 Frame Rate`,`F-BSY Frame Rate`,`F-RJT Frame Rate`, `Port Send Bandwidth Percentage`,")+
                         String.Format("`Port Receive Bandwidth Percentage`, Overall Port Bandwidth Percentage`,`Primitive Sequence Protocol Error Rate`,")+
                         String.Format("`Invalid Transmission Word Rate`,`Link Reset Transmitted Rate`,`Link Reset Received Rate`)")+ 
                          String.Format("SET Slot = nullif(@the_slot,''),")+ 
                          String.Format(@"Time= str_to_date(@the_time,'%m/%d/%y %h:%i %p')");

I have tried this, it did not work.

  string tester = files.FirstOrDefault().Directory.ToString();
        tester = tester.Replace(@"\\", @"\");
4

1 回答 1

0

好吧,我没有找到问题的答案,但我找到了解决方法。我想使用 BulkLoader 做的工作:

var bl = new MySqlBulkLoader(sqlconnect);
      bl.TableName = "by_switch";
      bl.FieldTerminator = ",";
     bl.LineTerminator = "\n";
      bl.FileName = files.FirstOrDefault().ToString();
       bl.NumberOfLinesToSkip = 1;
    int inserted = bl.Load();
于 2012-09-25T02:09:05.067 回答