0

运行任何类型的删除语句会导致 Invalid Statement 异常的非常奇怪的错误。

我的查询是delete from table where Id = 1。Id 不是主键。我还需要运行几个不使用主键的删除语句。完全相同的语句在 toad 上运行。

连接字符串:

<add name="Oracle_xxxxx" connectionString="Driver={Microsoft ODBC for Oracle};Server=
(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=*host*)(PORT=*port*))(CONNECT_DATA=
(SID=*sid*)));Uid=*uid*;Pwd=*pwd*;" providerName="System.Data.Odbc" />

查询运行良好。如果可能的话,我真的不想更改连接字符串/驱动程序。

错误信息 :

A first chance exception of type 'System.Data.Odbc.OdbcException' occurred in System.Data.dll
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String
method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String  
method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Odbc.OdbcCommand.ExecuteReader()
at ClientOnBoarding.COBTrackerDataManager.*function*() in C:\Working\SVN\xx\App_Code\xx.cs:line 928

ERROR [42000] [Microsoft][ODBC driver for Oracle][Oracle]ORA-00900: invalid SQL statement

任何帮助将不胜感激!

编辑:我首先运行 ExecuteNonQuery(),然后测试 ExecuteReader() 并且都给了我同样的错误 - 对上面的误导性错误消息表示歉意。

编辑 2:我已经在 Toad 上多次尝试和测试我的 SQL,没有任何问题。无论有没有尾随分号,我的更新和插入语句都可以正常工作。只有删除给了我一个例外。

编辑 3:

OdbcCommand cmd = new OdbcCommand("@delete from table where Id = " + param, conn);
//conn is OdbcConnection object with valid connection I use for all other commands. Conn is open. param is a string (which is a number)
try
{
    //cmd.ExecuteReader();//was testing both
    cmd.ExecuteNonQuery();
}//end try
catch (Exception e)
{
    System.Diagnostics.Debug.WriteLine(e.StackTrace);
    System.Diagnostics.Debug.WriteLine(e.InnerException);
    System.Diagnostics.Debug.WriteLine(e.Message);
}//end catch
4

2 回答 2

1

好吧,伙计们觉得自己像个白痴……@符号在引用之后而不是之前……

OdbcCommand cmd = new OdbcCommand("@delete from table where Id = " + param, conn);

应该

OdbcCommand cmd = new OdbcCommand(@"delete from table where Id = " + param, conn);
于 2012-06-27T04:06:14.547 回答
1

从错误消息看起来这是一个 sql 语句错误,与 .NET 无关。尝试直接在 oracle 中运行此查询。另外,我认为在 Oracle 中你需要“;” 最后delete from table where Id = 1;,还要确保 Id 或表名不是关键字

于 2012-06-26T10:55:03.827 回答