2014-02-25 105 views
0

我用下面的方法來計算的數據:爲什麼參數化查詢不起作用雖然程序工作正常

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; 
      } 
     } 

它不帶任何時間,總是返回-1!儘管當我在SQL編輯器中使用相同參數運行它時,大約需要4分鐘才能完成並返回1

+0

您是否試過刪除'CommandTimeout'? – Dirk

+0

耶同樣的問題。它根本沒有時間!並返回-1! –

+0

好吧,我對Informix瞭解不多,但我會刪除存儲過程名稱周圍的多餘空格。請記住,存儲過程的返回值與ExecuteNonQuery()的返回值不同。 [documentation](http://pic.dhe.ibm.com/infocenter/idshelp/v117/index.jsp?topic=%2Fcom.ibm.net_cc.doc%2Fcom.ibm.swg.im.dbclient.adonet。 ref.doc%2Fdoc%2FDB2CommandClassExecuteNonQueryMethod.htm)我發現說:「對於所有其他類型的語句[除INSERT,UPDATE,DELETE語句],返回值是-1」。 – Dirk

回答

1

也許嘗試先清除參數,並且還可以使用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); 
+0

我這樣做,然後我得到以下異常! '錯誤[HY090] [Informix .NET provider]無效的字符串或緩衝區length.at res = myIfxCmd.ExecuteNonQuery();' –