更好地在C#中使用单个参数多次的方式 [英] better way of using a single parameter multiple times in c#

查看:165
本文介绍了更好地在C#中使用单个参数多次的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用预处理语句从数据库中查询数据新我遇到实施的C#专门OracleParameters参数的问题。



所以我们可以说我有以下SQL:

 字符串SQL = 
,其中t1.columnX = @parm和t2.columnY = @parm选择*
>

和代码是这样的:

 的OracleCommand CMD =新的OracleCommand(SQL,康恩); 
cmd.Parameters.Add(新的OracleParameter(@ PARM,strParm));

在cmd中被执行t1.columnX得到strParm但t2.columnY时的价值问题是只是即将获得strParm的价值,它抛出一个ORA-01008:未绑定的所有变量异常。



在我看来,这个参数到达仅即使该参数在SQL别的地方见过一次取代。



一个解决方法我试过和工作对我来说是这样的:

 的OracleCommand CMD =新的OracleCommand(SQL,康涅狄格州) ; 
cmd.Parameters.Add(新的OracleParameter(@ PARM,strParm));
cmd.Parameters.Add(新的OracleParameter(@ PARM,strParm));



另一种解决方案是这样的:

  CMD的OracleCommand =新的OracleCommand(SQL,conn);在
cmd.Parameters.Add(新的OracleParameter(@ PARM1,strParm));
cmd.Parameters.Add(新的OracleParameter(@ parm2,strParm));

和修改后的SQL是这样的:

  SQL字符串=选择表1 T1 * 
,表2的T2
,其中t1.columnX = @ PARM1和t2.columnY = @ parm2

现在的问题是,是否有这样做,这样我就不用加其他参数与同一个更好的方法值。



请注意:我刚刚简化上面的查询显示,@parm在查询的几个部分使用。在现实生活中这个查询使用了相同的参数多次,它是一个痛苦必须添加相同的名称和值的多个参数。


解决方案

我有同样的问题前,IIRC这解决了它:

  cmd.BindByName = TRUE; 



编辑:我刚刚重新检查,这是否让你即使设置参数值一次虽然参数可以在查询中指定多次。那我做不同的你唯一的其他事情是,我与一家领先的指定的参数名:,例如:参数


I'm new in using prepared statements for querying data from the database and I'm experiencing problems implementing parameters for c# specifically OracleParameters.

So let's say I have the following SQL:

string sql = "select * 
              from table1 t1, table2 t2 
              where t1.columnX = @parm and t2.columnY = @parm"

And the code is this:

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.Parameters.Add(new OracleParameter("@parm", strParm));

The problem is when the cmd gets executed t1.columnX gets the value of strParm but when t2.columnY is just about to get the value of strParm, it throws an "ORA-01008: not all variables bound" exception.

It seems to me that the parameter gets to be substituted only once even though that parameter is seen somewhere else in the sql.

One solution I tried and works for me is this:

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.Parameters.Add(new OracleParameter("@parm", strParm));
cmd.Parameters.Add(new OracleParameter("@parm", strParm));

Another solution is this:

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.Parameters.Add(new OracleParameter("@parm1", strParm));
cmd.Parameters.Add(new OracleParameter("@parm2", strParm));

and the modified sql is this:

string sql = "select * 
             from table1 t1, table2 t2 
             where t1.columnX = @parm1 and t2.columnY = @parm2" 

The question is, is there a better way of doing this so that I don't have to add another parameter with the same value.

Note: I've just simplified the query above to show that @parm is used in several parts of the query. In real life this query has used the same parameter several times and its a pain to have to add more than one parameter of the same name and value.

解决方案

I've had the same issue before and IIRC this solved it:

cmd.BindByName = true;

EDIT: I've just re-checked and this does allow you to set the parameter value once even though the parameter may be specified multiple times in the query. The only other thing that I'm doing differently to you is that I specify the parameter names with a leading :, for example :param.

这篇关于更好地在C#中使用单个参数多次的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆