使用REGEXP_LIKE准备语句对OraOLEDB.ORACLE

时间:2014-03-08 04:26:08

标签: c# oracle prepared-statement sql-like

我有一个奇怪的情况。预准备语句返回0行,使用字符串连接构建时使用相同的语句返回预期的15行。

string mappingsTable = "MySchema.MyMappingsTable";
string sourceTablename = "Zone1_12345";
string destinTablename = "SurveyABC_result";

这个工作并返回15行

DataTable mappings = new DataTable();
OleDbCommand cmd = MyOleDbConnection.CreateCommand();

cmd.CommandText 
  = "select * from " + mappingsTable 
  + " where regexp_like('" + sourceTablename + "', SourceNamePattern, 'i')"
  + " and UPPER(destinTablename) = UPPER('" + destinTablename + "')";

new OleDbDataAdapter(cmd).Fill(mappings);

这个运行(没有例外)并返回0个零行!@#$%? GRR

DataTable mappings = new DataTable();
OleDbCommand cmd = MyOleDbConnection.CreateCommand();

cmd.CommandText 
  = "select * from " + mappingsTable
  + " where regexp_like(?, SmHeatLikePattern, 'i')"
  + " and UPPER(destinTablename) = UPPER(?)";

cmd.Parameters.Add(new OleDbParameter(sourceTablename, OleDbType.VarChar));
cmd.Parameters.Add(new OleDbParameter(destinTableName, OleDbType.VarChar));

new OleDbDataAdapter(cmd).Fill(mappings);

为什么准备好的语句没有行的任何想法?我不知所措:(

1 个答案:

答案 0 :(得分:0)

您使用的OleDbParameter的构造函数重载将参数名称和参数类型作为输入。所以基本上你错过了传递实际值。更改代码如下所示:

DataTable mappings = new DataTable();
OleDbCommand cmd = MyOleDbConnection.CreateCommand();

cmd.CommandText 
  = "select * from " + mappingsTable
  + " where regexp_like(?, SmHeatLikePattern, 'i')"
  + " and UPPER(destinTablename) = UPPER(?)";

OleDbParameter param1 = cmd.Parameters.Add(sourceTablename, OleDbType.VarChar);
param1.Value = sourceTablename;

OleDbParameter param2 = cmd.Parameters.Add(destinTableName, OleDbType.VarChar);
param2.Value = destinTableName;

new OleDbDataAdapter(cmd).Fill(mappings);