多次使用相同的绑定变量

时间:2014-04-25 07:26:33

标签: c# oracle variables binding

如何多次使用相同的绑定变量。 有一个SQL查询:

select max(db.p_code || ' - ' || db.p_longname) as p_db,
       max(o.p_bankoperationid) as p_bankoperationid,
       max(to_char(o.p_operationdatetime, 'DD.MM.YYYY hh24:mi:ss')) as p_operationdatetime
  from tb_offlineoperations o,
       tb_dict_database     db,
       tb_dict_currency     cur,
       tb_off_members       sm,
       TB_DICT_MEMBER_TYPE  mrol,
       tb_dict_doccategory  cd
 where o.p_issuedbid = db.p_code(+)
   and o.p_currencycode = cur.p_code(+)
   and o.id = sm.p_operationid(+)
   and sm.p_clientrole = mrol.p_code(+)
   and o.p_doccategory = cd.p_code(+)
   and (trunc(o.p_operationdatetime) between :P1 and :P2)
   and (sm.p_account = :P3 or :P3 is null)
   and (o.p_baseamount between :P4 and :P5 or (coalesce(:P5, 0) = 0))
   and (o.p_currencyamount between :P6 and :P7 or (coalesce(:P7, 0) = 0))
   and (o.p_currencycode = :P8 or :P8 is null)
 group by o.id
 order by max(o.p_bankoperationid)

在C#中执行此代码我使用

QueryString = reportSQL;
            OracleCommand c = new OracleCommand(QueryString, connection);
            c.CommandType = CommandType.Text;

            //добавляем связанные переменные
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                OracleParameter repParam = new OracleParameter();
                repParam.ParameterName = (string)dr.Cells[2].Value;
                switch ((string)dr.Cells[3].Value)
                {
                    case "D":
                        repParam.OracleDbType = OracleDbType.Date;
                        if (!DBNull.Value.Equals(dr.Cells[1].Value))                            
                            repParam.Value = DateTime.ParseExact((string)dr.Cells[1].Value, "dd.MM.yyyy", CultureInfo.InvariantCulture);                            
                        else
                            repParam.Value = dr.Cells[1].Value;
                        break;
                    case "N":
                        repParam.OracleDbType = OracleDbType.Decimal;
                        repParam.Value = dr.Cells[1].Value;
                        break;
                    case "V":
                        repParam.OracleDbType = OracleDbType.Varchar2;
                        repParam.Value = dr.Cells[1].Value;
                        break;
                }

                c.Parameters.Add(repParam);
            }

            OracleDataAdapter da = new OracleDataAdapter(c);
            dataSet = new DataSet();

            da.Fill(dataSet, "Table");

参数在表格中,看起来像

Select param_name as \"Param name\",null as \"Value\",name,PARAM_TYPE from tb_report_parameters t 
1   start date      P1  D
2   end date        P2  D
3   account         P3  V
4   sum from        P4  N
5   sum to          P5  N
6   curr sum from   P6  N
7   curr sum to     P7  N
8   currency code   P8  V

执行时我有ORA-01008: not all variables bound。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

明白是什么原因。变量按顺序绑定,当时,我认为它们是按名称绑定的。添加c.BindByName = true;