关于 mysql:Error in Load Data infile query in C#

Error in Load Data infile query in C#

以下代码有问题:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 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 ‘/
/
‘(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.locationFIELDS TERMINATED BY ‘–>’LINES TERMINATED BY ‘//
//
‘(条形码,BinLoc);”;
但是当文件路径在字符串变量中传递时会引发错误。

错误:-
错误 [HY000] [MySQL][ODBC 5.1 驱动程序][mysqld-5.1.48-community] 找不到文件 ‘D:HHTFilesABC.txt’ (Errcode: 2)


@Aghilas 它得到了解决:) 虽然我不确定为什么我首先会出错。 Anway,我只是将 ‘////////’ 替换为 ‘/’ 并且运行良好。

1
2
3
4
5
6
7
        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 ‘/
/
‘(Barcode,BinLoc);"
);

尝试使用 Path.Combine

1
Path.Combine("D:////HHTFiles/", nm);

并使用 StringBuilder 来构建您的查询

1
2
3
4
5
6
7
8
9
10
 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();


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/269593.html

(0)
上一篇 2022年6月22日
下一篇 2022年6月22日

相关推荐

发表回复

登录后才能评论