[英]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.