简体   繁体   English

尽管过程正常,但为什么参数化查询不起作用

[英]Why parameterized query doesn't work although the procedure works fine

I use the following method to calculate data: 我使用以下方法来计算数据:

  public static int PrepareData(int year, int month, int calcYear)
        {
            using (IfxConnection con = new IfxConnection(ConfigurationManager.ConnectionStrings["testable"].ToString()))
            {
                int res = 0;
                StringBuilder cmdTxt = new StringBuilder();
                cmdTxt.Append(" hk_calc_data ");
                using (var myIfxCmd = new IfxCommand(cmdTxt.ToString(), con))
                {
                    myIfxCmd.CommandType = CommandType.StoredProcedure;

                    myIfxCmd.Parameters.Add("p_year", IfxType.Integer);
                    myIfxCmd.Parameters.Add("p_month", IfxType.Integer);
                    myIfxCmd.Parameters.Add("p_calc_year", IfxType.Integer);

                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    myIfxCmd.Parameters[0].Value = year;
                    myIfxCmd.Parameters[1].Value = month;
                    myIfxCmd.Parameters[2].Value = calcYear;
                    myIfxCmd.CommandTimeout = 1000;
                    res = myIfxCmd.ExecuteNonQuery();
                }
                con.Close();
                con.Dispose();
                return res;
            }
        }

It takes no time and always returns -1 ! 它不花时间,总是返回-1 Although when I ran it in SQL editor with the same parameters, it takes about 4 minutes to complete and return 1 as result! 尽管当我在具有相同参数的SQL编辑器中运行它时,它大约需要4分钟才能完成并返回1

Maybe try clearing the params first, and also adding value with the Add. 也许首先尝试清除参数,然后使用“添加”增加价值。

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                myIfxCmd.Parameters.Clear();
                myIfxCmd.Parameters.Add("p_year", IfxType.Integer, year);
                myIfxCmd.Parameters.Add("p_month", IfxType.Integer, month);
                myIfxCmd.Parameters.Add("p_calc_year", IfxType.Integer, calcYear);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询有效,但将其放入存储过程时不起作用 - Query works but when put it in stored procedure doesn't work 参数化查询不起作用,没有错误 - Parameterized query doesn't work, no errors 存储过程在查询生成器中执行时有效,但不适用于C#代码 - Stored procedure works when executed in query builder, but doesn't work from C# code Aero Glass在Windows 7上可以正常工作,但在Vista上则不能工作 - Aero Glass works fine on Windows 7, but doesn't work on Vista HttpClient PostAsync() 不起作用,而 POSTMAN 工作正常 - HttpClient PostAsync() doesn't work while POSTMAN works fine Call to Process可与Debug一起正常使用,但在已安装的应用程序中不起作用 - Call to Process works fine with Debug, but it doesn't work in the installed application 我的WebApi在Visual Studio上运行良好,但在IIS上不起作用 - My WebApi works fine on Visual Studio but doesn't work on IIS 为什么在调用process.start时StartInfo.stuff不起作用,但是Process.start在C#中可以正常工作 - Why StartInfo.stuff doesn't work when calling process.start but Process.start works fine in C# 为什么在Func中解构 <KeyValuePair,T> 尽管具有Deconstruct扩展方法,但仍不起作用? - Why deconstructing in Func<KeyValuePair,T> doesn't work, although having a Deconstruct extension method? 为什么这个LINQ查询不起作用? - Why doesn't this LINQ Query Work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM