執(zhí)行ADO測試程序相關使用說明
原來在Sql Server及Oracle上操作時,執(zhí)行完成后直接把連接關閉了。返回的參數(shù)想怎么處理就怎么處理,并不會出錯,注意使用COM交互操作訪問ADO的數(shù)據(jù)將極大的降低性能。
不知道這里有多少人用Sybase做數(shù)據(jù)庫開發(fā)的,我是有點受不了了。用了ADO測試程序,沖破重重阻力,終于系統(tǒng)算是正常運行了,要做更進一步的處理:增加數(shù)據(jù)權限。我們原來是在Sql Server上用函數(shù)直接返回的結果集來判斷的。#t#
但是Sybase不支持函數(shù),只好用存儲過程返回一個判斷權限的條件字符串動態(tài)執(zhí)行。問題是取出輸出參數(shù)總提提示“Command has been closed”。奇怪啊,明明執(zhí)行的時候是Open了Connection了。寫了一個ADO測試程序:
- 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) ;
由于是我們有一個數(shù)據(jù)層專門處理數(shù)據(jù)庫操作,開始懷疑封裝的不好了。檢查了N次也不知道在哪出錯,ADO測試程序總以為是輸出參數(shù)類型有問題,試過幾次,長度也改過。不行。
后來注意到:跟蹤執(zhí)行的時候執(zhí)行成功,總是在取參數(shù)的時候出錯。
再看一下錯誤信息“Command has been closed”,暈!ADO測試程序不會是取參數(shù)值的時候要求數(shù)據(jù)庫連接保持Open狀態(tài)吧? 修改一下測試代碼:
- 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() ; //提前關閉數(shù)據(jù)庫
- Console.WriteLine(com.Parameters["@returnSql"].Value) ;