我有下面的代碼的問題: -錯誤在C#LOAD DATA INFILE查詢
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);
}
}
}
它運行正常,如果我給完整的文件路徑在我的查詢,如: - 字符串QRY =「 LOAD DATA INFILE'D:\ HHTFiles \ ABC.txt'INTO TABLE table1
。location
FIELDS TERMINATED BY' - >'LINES TERMINATED BY'\ r \ n'(Barcode,BinLoc);「; 但是,當文件路徑傳遞給String變量時,它會引發錯誤。
錯誤: - ERROR [HY000] [MySQL的] [ODBC 5.1驅動]的[mysqld-48年5月1日社區]文件 'd:HHTFilesABC.txt' 未找到(ERRCODE:2)
感謝您的快速回復。我試過你的代碼,但我仍然得到了以前的錯誤。錯誤[HY000] [MySQL] [ODBC 5.1驅動程序] [mysqld-5.1.48-community]找不到文件'D:HHTFilesABC.txt'(Errcode:2) – RookieCoder 2012-07-25 12:26:19
高興地回覆你,驗證D:HHTFiles/ABC。 txt存在路徑 – 2012-07-25 12:30:50
是的,ABC.txt文件存在於指定的路徑中。我甚至在上面的代碼段上應用了斷點。它顯示qry =「LOAD DATA INFILE'D:\\ HHTFiles \\ ABC.txt'INTO TABLE 'table1'.'location'FIELDS TERMINATED BY' - >'LINES TERMINATED BY'\ r \ n'(Barcode,BinLoc);「。如果我在Mysql Editor上運行這個查詢,它運行正常並且1行受到影響。但相同的查詢不在C#中工作。我很困惑... – RookieCoder 2012-07-26 05:56:04