0

この不穏なエラーに遭遇しました。C# で ODP.NET を使用して、単純な SQL クエリを実行しようとしています。パラメータを名前で渡します (コマンドの BindByName=true を設定)。クエリでは、「tid」という名前のパラメータを 1 つ使用します。このパラメーターのみをコマンドのパラメーター コレクションに追加すると、すべて問題ありません。クエリで使用されていない別のパラメーターを追加すると、クエリがクラッシュし、このエラー メッセージが表示されます。

ORA-01036: illegal variable name/number

コードは次のようになります

using( var conn = new OracleConnection([some connection string]) )
{
   conn.Open();
   using( var comm = conn.CreateCommand() )
   {
      // using only the :tid parameter.
      comm.CommandText = "SELECT column FROM Table T WHERE T.Id = :tid";

      comm.CommandType = CommandType.Text;
      comm.BindByName = true;

      comm.Parameters.Add("tid", 500000207);

      // This extra parameter causes an exception when the query is executed.
      // If I remove it everything runs smoothly
      comm.Parameters.Add("param2", "ValueOfSecondParam");

      comm.ExecuteNonQuery();
   }
}

例外を取得せずに、クエリで実際に使用されているよりも多くのパラメーターを渡すにはどうすればよいですか? (クエリを動的に作成するとしますが、渡すパラメーターを制御しないため、最初にすべてのパラメーターを渡す必要があります)

4

1 に答える 1

4

SQL クエリにパラメーターを提供する代わりに、車を組み立てようとしているメカニックに部品を渡すことを想像してみてください。あなたが Bud Abbott のその部分を演じており、データベース/メカニックが Lou Costello であると仮定します。

Abbott:   Alright, Lou, let's get busy putting this car together.  The owner's
          in a big rush.
Costello: Sounds good to me.
Abbott:   Here's the steering wheel.
Costello: Thank you very much.
A:  And here's the engine.
C:  (Ooof!) Oh, thank you SO very much!
A:  And now the first wheel.
C:  That's great.
A:  And now the second wheel.
C:  Looks all nice and shiny with all the chrome, eh?
A:  That's just lovely, Lou.  Oh, here's the third wheel.
C:  I'll just put in back here, behind the driver.
A:  Fine, fine.  And here's the fourth wheel...
C:  Looks great!  I think we're...
A:  And here's the fifth wheel...
C:  Uh, hold on here a minute...
A:  And here's the sixth wheel...
C:  Hang on, Bud, I think we've got a little problem here...
A:  And now the seventh wheel...
C:  Seven wheels?  Seven wheels?!?  WHADDYA MEAN, SEVEN WHEELS!?!?!?
A:  Oh, stop complaining, will you?  Just put it on...
C:  But...
A:  Hurry up now...
C:  But...
A:  ...because here's the eighth wheel.
C:  HEY, ABBBBBOTTTTTTTTTT!!!!!!!

ご覧のとおり、より多くのホイール (またはパラメーター) を提供することはあまり意味がありませんが素晴らしいコメディー ルーチンにはなる可能性があります。:-)

共有してお楽しみください。

于 2012-09-05T17:17:22.343 に答える