資源使用優化有一個範例: - 儘早獲取併發布。考慮這個範例。我總是看到下面的序列DB調用:資源優化問題.net
- 開放連接錯誤
- 創建命令
- 做驗證
- 設定參數
- 最後執行命令
是,如果我anythng錯做?
- 創建命令
- 做驗證
- 設定參數
- 開放連接錯誤
- 最後執行命令
e.g
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "cus";
cmd.CommandType = CommandType.StoredProcedure;
//Configure input parameters
SqlParameter param = new SqlParameter();
param = cmd.Parameters.Add(new SqlParameter("@id", 2));
param.Direction = ParameterDirection.Input;
SqlConnection conn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI; Initial Catalog=SpringApp;");
conn.Open();
cmd.Connection = conn;
cmd.Prepare();
SqlDataReader reader = cmd.ExecuteReader();
YOu可以編輯您的問題 – Blorgbeard 2012-02-15 17:09:50
請務必關閉您的連接 – gprasant 2012-02-15 17:15:25