我遇到以下代码问题: -
private void btnUpload_Click(object sender, EventArgs e)
{
string nm = txtFilename.Text;
string qry = "LOAD DATA INFILE 'D:\\HHTFiles\\" + nm + "' INTO TABLE `table1`.`location`FIELDS TERMINATED BY '-->'LINES TERMINATED BY '\r\n'(Barcode,BinLoc);";
cmd = new OdbcCommand(qry, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show(" File loaded successfully...");
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledailog1 = new OpenFileDialog();
openfiledailog1.InitialDirectory = "D:\\HHTFiles\\";
openfiledailog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openfiledailog1.FilterIndex = 2;
openfiledailog1.RestoreDirectory = true;
if (openfiledailog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((openfiledailog1.OpenFile()) != null)
{
txtFilename.Text = openfiledailog1.SafeFileName.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
如果我在查询中提供完整的文件路径,它运行正常,如: -
string qry =“LOAD DATA INFILE'D:\ HHTFiles \ ABC.txt'INTO TABLE table1
。location
FIELDS TERMINATED BY' - >'LINESATED by'\ r \ n'(条码,BinLoc);“;
但是,当在String变量中传递文件路径时,它会抛出错误。
错误: - 错误[HY000] [MySQL] [ODBC 5.1驱动程序] [mysqld-5.1.48-community]找不到文件'D:HHTFilesABC.txt'(错误代码:2)
答案 0 :(得分:2)
@Aghilas它解决了:)虽然我不知道为什么我在第一时间出错。不管怎样,我只是用'/'替换'\\',它运行正常。
string nm = txtFilename.Text;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("LOAD DATA INFILE ");
stringBuilder.Append(Path.Combine("'D:/HHTFiles/", nm));
stringBuilder.Append("' INTO TABLE `table1`.`location`FIELDS TERMINATED BY '-->'LINES TERMINATED BY '\r\n'(Barcode,BinLoc);");
答案 1 :(得分:0)
尝试使用Path.Combine
Path.Combine("D:\\HHTFiles\\", nm);
使用StringBuilder构建查询
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("LOAD DATA INFILE ");
stringBuilder.Append(Path.Combine("D:\\HHTFiles\\", nm));
stringBuilder.Append(" INTO TABLE table1.location");
stringBuilder.Append(" FIELDS TERMINATED BY '-->'");
stringBuilder.Append(" LINES TERMINATED BY (");
stringBuilder.Append(Barcode.BinLoc);
stringBuilder.Append(")");
string qry = stringBuilder.ToString();