Sybase Ase ADO.NET輸出參數

來源:互聯網
上載者:User
不知道這裡有多少人用Sybase做資料庫開發的,我是有點受不了了。用了Sybase ASE ADO.NET,衝破重重阻力,終於系統算是正常運行了,要做更進一步的處理:增加資料許可權。我們原來是在Sql Server上用函數直接返回的結果集來判斷的,但是Sybase不支援函數,只好用預存程序返回一個判斷許可權的條件字串動態執行。問題是取出輸出參數總提提示“Command has been closed”。奇怪啊,明明執行的時候是Open了Connection了。寫了一個測試程式:

   AseConnection con = new AseConnection("Data Source='SYBASE'; Port=5000; UID='sa'; PWD=''; Database='data';Connection Timeout='300';") ;

   AseCommand com = new AseCommand("GetDataRightSQL", con) ;

   com.CommandType = System.Data.CommandType.StoredProcedure ;

   try

   {

    AseParameter prm = new AseParameter("@UserID", 1) ;

    //   prm.Direction = System.Data.ParameterDirection.Input ;

    com.Parameters.Add(prm) ;

    prm = new AseParameter("@Category","Department") ;

    com.Parameters.Add(prm) ;

    com.Parameters.Add(new AseParameter("@FieldName", "Dept_ID")) ;

    prm = new AseParameter("@returnSql", AseDbType.VarChar, 250) ;

    prm.Direction = System.Data.ParameterDirection.Output ;

    com.Parameters.Add(prm) ;

    con.Open() ;

    com.ExecuteNonQuery() ;

    Console.WriteLine(com.Parameters["@returnSql"].Value) ;

    

   }

   finally

   {

    if( con != null && con.State == System.Data.ConnectionState.Open )

     con.Close() ;

   }

   return ;

    FT,沒有問題啊,返回的結果顯示出來了,資料正確。

由於是我們有一個資料層專門處理資料庫操作,開始懷疑封裝的不好了。檢查了N次也不知道在哪出錯,總以為是輸出參數類型有問題,試過幾次,長度也改過。不行。

後來注意到:跟蹤執行的時候執行成功,總是在取參數的時候出錯。再看一下錯誤資訊“Command has been closed”,暈!不會是取參數值的時候要求資料庫連接保持Open狀態吧?

修改一下測試代碼:

   AseConnection con = new AseConnection("Data Source='SYBASE'; Port=5000; UID='sa'; PWD=''; Database='data';Connection Timeout='300';") ;

   AseCommand com = new AseCommand("GetDataRightSQL", con) ;

   com.CommandType = System.Data.CommandType.StoredProcedure ;

   try

   {

    AseParameter prm = new AseParameter("@UserID", 1) ;

    //   prm.Direction = System.Data.ParameterDirection.Input ;

    com.Parameters.Add(prm) ;

    prm = new AseParameter("@Category","Department") ;

    com.Parameters.Add(prm) ;

    com.Parameters.Add(new AseParameter("@FieldName", "Dept_ID")) ;

    prm = new AseParameter("@returnSql", AseDbType.VarChar, 250) ;

    prm.Direction = System.Data.ParameterDirection.Output ;

    com.Parameters.Add(prm) ;

    con.Open() ;

    com.ExecuteNonQuery() ;

    con.Close() ; //提前關閉資料庫

    Console.WriteLine(com.Parameters["@returnSql"].Value) ;

    

   }

   finally

   {

    if( con != null && con.State == System.Data.ConnectionState.Open )

     con.Close() ;

   }

   return ;

哈哈,果真出錯了。

原來在Sql Server及Oracle上操作時,執行完成後直接把串連關閉了。返回的參數想怎麼處理就怎麼處理,並不會出錯。但是沒有想到在Sybase ASE ADO.NET中竟然要求操作AseCommand的Parameters時要求資料庫操持串連。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.