现在的位置: 首页 > 综合 > 正文

奇怪問題 SqlCommand的CommandText賦值時機產生的性能差異

2011年10月10日 ⁄ 综合 ⁄ 共 766字 ⁄ 字号 评论关闭

最近在做一個項目的數據庫性能檢測時發現,在SqlProfiler中不斷出現頻繁Audit Login和Audit Logout,經檢查在執行數據的Update 或Add操作時如果Command語句的CommandText賦值在賦參數值之前不會Logout ,如果賦值在後則會執行Logout,不知這是何原因,其中性能的差異將可達到15% -20%

如果寫法如下:
創建連接Conn;
SQLCommand comm = new SQLCommand(conn);
SQLParameter param ;
string strSQL= "Update table1 set name=@name"
comm.CommandText=strSQL;         //注意:此語句賦值在Parameter之前
param.ParameterName = "@Name";
param.Value = "value";
comm.Parameters.Add(param);
comm.ExecuteNonQuery();
conn.Close(); //連接池有使用,數據庫連接未Logout

如果寫法如下
SQLCommand comm = new SQLCommand(conn);
SQLParameter param =new SQLParameter();
string strSQL= "Update table1 set name=@name"
param.ParameterName = "@Name";
param.Value = "value";
comm.Parameters.Add(param);
comm.CommandText=strSQL;            //注意:此語句在賦值Parameters之後
comm.ExecuteNonQuery();
conn.Close(); //數據庫連接Logout

 

 

抱歉!评论已关闭.