在C#中使用OleDbCommand运行INSERT命令的问题

时间:2016-08-09 04:44:34

标签: c# oledbcommand

我有以下看似没问题,但由于某种原因,它会抛出ORA-00936: missing expression

...
var query = "INSERT INTO MY_TABLE VALUES (@id, @filepath, @filename, @filesize, @md5)";

using (var insertCmd = new OleDbCommand(query, conn))
{
    insertCmd.CommandType = CommandType.Text;
    insertCmd.Parameters.AddRange(new OleDbParameter[]
    {
        new OleDbParameter("@id", FindItemId(filePath)),
        new OleDbParameter("@filepath", filePath),
        new OleDbParameter("@filename", new FileInfo(filePath).Name),
        new OleDbParameter("@filesize", new FileInfo(filePath).Length),
        new OleDbParameter("@md5", GetMd5Hash(filePath))
    });
    insertCmd.ExecuteNonQuery();
}
...

enter image description here

在调试中,我可以看到参数值是否正确绑定。那么我做错了什么?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

Mick上面说的是正确的。

OleDbCommand和OdbCommand不支持命名参数并使用?而是占位符,因此参数的顺序很重要。但是,出于以下可读性目的,您可以为其参数指定名称而不是使用?:

...
var query = "INSERT INTO MY_TABLE VALUES (?, ?, ?, ?, ?)";

using (var insertCmd = new OleDbCommand(query, conn))
{
insertCmd.CommandType = CommandType.Text;
insertCmd.Parameters.AddRange(new OleDbParameter[]
{
    new OleDbParameter("@id", FindItemId(filePath)),
    new OleDbParameter("@filepath", filePath),
    new OleDbParameter("@filename", new FileInfo(filePath).Name),
    new OleDbParameter("@filesize", new FileInfo(filePath).Length),
    new OleDbParameter("@md5", GetMd5Hash(filePath))
});
insertCmd.ExecuteNonQuery();
}
...

答案 1 :(得分:0)

好的找到答案here

显然需要在ORACLE 0_o中使用?与OLEDB绑定

以下作品..

...
var query = "INSERT INTO MY_TABLE VALUES (?, ?, ?, ?, ?)";

using (var insertCmd = new OleDbCommand(query, conn))
{
    insertCmd.CommandType = CommandType.Text;
    insertCmd.Parameters.AddRange(new OleDbParameter[]
    {
        new OleDbParameter("?", FindItemId(filePath)),
        new OleDbParameter("?", filePath),
        new OleDbParameter("?", new FileInfo(filePath).Name),
        new OleDbParameter("?", new FileInfo(filePath).Length),
        new OleDbParameter("?", GetMd5Hash(filePath))
    });
    insertCmd.ExecuteNonQuery();
}
...