[英]Is it okay to call OracleConnection.Open() if the connection is already open?
IOW,有沒有一種標准的測試方法,la:
if (! con.Open())
con.Open();
?
更新:我想我沒有提到這些是我正在使用的Oracle的ODP組件。 OracleConnection組件沒有ConnectionState屬性。
使用Connection.State屬性檢查連接是否已打開
if(con.State == ConnectionState.Closed){
con.Open();
}
if(Con.State != ConnectionState.Open)
con.Open();
如果你想打開只能關閉的連接,你可以使用
if(Con.State == ConnectionState.Close)
con.Open();
ConnectionState: http : //msdn.microsoft.com/en-us/library/system.data.connectionstate.aspx
您需要將代碼分成不同的層以避免這種情況。 連接應僅在main方法中打開(在API中 - 不是主要的應用程序方法) - 然后傳遞給僅使用連接而不管理連接的協調(私有)方法(打開,關閉,事務操作等) )。 這樣可以保持代碼清潔,保證事務/連接的安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.