努力在 C# 中使用绑定变量

Posted

技术标签:

【中文标题】努力在 C# 中使用绑定变量【英文标题】:Struggling to use bind variable in C# 【发布时间】:2021-06-14 12:22:14 【问题描述】:

我正在尝试在 C# 中使用绑定变量来获取选择查询中的记录。以下代码是我尝试过的,但出现异常: ORA-01006: bind variable does not exist 。我不知道变量不存在或其他什么地方在哪里?

string sleeveListQuery = @"select col from table where id = :V1  :V2 ";
    
                    inClause="some condition";                
                    List<SleeveSearch> sleeveSearchList= new List<SleeveSearch>();
                    using (OracleConnection objConn = new OracleConnection(ConnectionString))
                    
                        objConn.Open();
                        using (var command = objConn.CreateCommand())
                        
                            command.CommandText = sleeveListQuery;
                            command.Parameters.Add(":V1", OracleDbType.Int32, Int32.Parse(univId), ParameterDirection.Input);
                            command.Parameters.Add(":V2", OracleDbType.Varchar2, inClause, ParameterDirection.Input);
                            OracleDataReader dr = command.ExecuteReader();
                            while (dr.Read())
                            
                                SleeveSearch dataRow = new SleeveSearch();
                                dataRow.SleeveName = dr["SLEEVENAME"].ToString();
                                sleeveSearchList.Add(dataRow);
                            
                        
                        
                    

【问题讨论】:

【参考方案1】:

您正在将参数添加到命令中,但您没有为它们提供任何值。即:

//...
command.Parameters[":V1"].Value = 10;
command.Parameters[":V2"].Value = "Hello";
//...

PS:可能你的意思是:

... where id = :V1 and something = :V2

【讨论】:

command.Parameters.Add(":V1", OracleDbType.Int32, Int32.Parse(univId), ParameterDirection.Input);这不增加价值吗?我以为这样做了 @ShaliniRaj,哦,我看到你也有 value 参数。我错过了。我更喜欢风格 Parameters.Add(parameterName, parameterType).Value = ... 但无论如何,你的语法也是错误的。

以上是关于努力在 C# 中使用绑定变量的主要内容,如果未能解决你的问题,请参考以下文章

如何反序列化 Json 并绑定到变量/属性 c#

如何判断变量的值是否是绑定到Scheme中过程的符号?

c#的menustrip绑定contextmenustrip

c++和c#的Visual Studio键绑定[重复]

Oracle“ORA-01008:并非所有变量都绑定”错误带参数

为啥我不能在立即执行语句中使用绑定变量?