0

我正在尝试将 CSV 文件加载到 C# 中的 MySQL 数据库中,但我不断收到无法从结果集中读取的异常。我在 MySQL 工作台中清理了语法,以至于我可以将 CSV 文件加载到 MySQL 工作台的数据库中而没有错误。在我的程序中尝试它,我仍然得到异常。我正在使用的文件具有以下路径和文件名:

D:\SANCentral\Customer Files\ibm\70738\0918\switch port.csv

我得到的例外是“无法从结果集中读取”。查看内部异常,我得到文件未找到。我不知道那是从哪里来的。D:\Projects\CSVParser 是我的项目的位置。我假设 MySQL.Data.Client 类正在使用我给它打开文件的位置做一些事情。这显然是 SQL 命令试图从中加载文件的路径:

 {"Could not find file  'D:\\Projects\\CSVParser\\CSVParser\\bin\\Debug\\SANCentralCustomer Filesibm70738'
.":"D:\\Projects\\CSVParser\\CSVParser\\bin\\Debug\\SANCentralCustomer Filesibm70738"}

我怎样才能解决这个问题?我在调试器中检查了要解析的文件的路径,它是正确的。我尝试将 .CSV 文件移动到从中运行项目的项目目录中,但仍然出现异常,但它是路径中的非法字符,而不是找不到文件。

这是执行加载的 C# 代码和 MySQL 语句:

string ConnectionString =  String.Format(@"server=localhost;userid={0};
        password={1};database=PerformanceMonitors;
Allow User Variables=True", user,password);
        MySqlConnection sqlconnect = new MySqlConnection(ConnectionString);
        sqlconnect.Open();

这是 MySQL 语句并执行查询。抱歉,字符串有点长:

string working = String.Format(@"LOAD DATA LOCAL INFILE '{0}' IGNORE
INTO TABLE {1} COLUMNS TERMINATED BY ',' LINES TERMINATED BY '\n'
  IGNORE 1 LINES (`Switch`,`Port`,`WWPN`,@the_slot,`Port Index`,@the_time,
`Interval`,`Port Send Packet Rate`,  
`Port Receive Packet Rate`,`Total Port Packet Rate`,`Port Send Data Rate`, 
`Port Receive Data Rate`,`Total Port Data Rate`, 
Peak Send Data Rate`,`Port Peak Receive Data Rate`,

Port Send Packet Size, Port Receive Packet Size, Overall Port Packet Size,
Error Frame Rate, Dumped Frame Rate,
Link Failure Rate, Loss of Sync Rate, Loss of Signal Rate, CRC Error Rate,
Short Frame Rate, Long Frame Rate, Encoding Disparity Error Rate,
Discarded Class3 Frame Rate, F-BSY Frame Rate, F-RJT Frame Rate,
Port Send Bandwidth Percentage, Port Receive Bandwidth Percentage, Overall Port Bandwidth Percentage, Primitive Sequence Protocol Error Rate,
Invalid Transmission Word Rate, Link Reset Transmitted Rate, Link Reset Received Rate)

    SET Slot = nullif(@the_slot,''), Time= str_to_date(@the_time,'%m/%d/%y %h:%i %p')", files.FirstOrDefault().ToString(), "by_switch");


       string commandreplaced= working.Replace("\n", "");

        MySqlCommand cmd = new MySqlCommand(commandreplaced,sqlconnect);
        cmd.ExecuteNonQuery();
4

1 回答 1

3

好吧,我终于想出了答案。LOAD DATA FILE 语句要求文件名是字符串值,它不能被参数化。不幸的是,MySQLCommand 类不喜欢 C# 字符串对象的文件路径中的转义 \,因此它无法找到该文件。我通过逃避逃逸来让它工作:

//build the LOAD DATA File command
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')");


// now escape the escape character
       commandreplaced = commandreplaced.Replace(@"\", @"\\");

   // now execute the command
        MySqlCommand cmd = new MySqlCommand(commandreplaced,sqlconnect);
于 2012-09-26T20:34:16.257 回答