這篇文章不僅對
他們會遇到什麼樣的問題?
上面的問題就像毒瘤
若要知道數據庫連接超時問題
[sample
Public Shared Function getOEMPN(ByVal psPN As String
Dim clsResult As New BSResult
Try
clsResult
Dim dtResult As New DataTable
Dim Sql As String = String
Dim clsOraDb As New clsOraClienDb
Dim strConn As String = ConfigurationManager
clsOraDb
Sql =
Dim params() As racleParameter = {New OracleParameter(
If clsOraDb
Return clsResult
End If
If dtResult Is Nothing Then
Return clsResult
End If
If dtResult
OEMPN = dtResult
Else
OEMPN =
End If
clsResult
Return clsResult
Catch ex As Exception
clsResult
Return clsResult
End Try
End Function
對上述代碼行的部分解釋
Dim clsOraDb As New clsOraClienDb:引用數據庫連接的類;
clsOraDb
然後
讓我們談談數據庫連接的問題
在Web這個程式裡
這還不算什麼
[sample
Foreach(DataRow row in tabl
……………
clsOraDb
…………
Next
有人還喜歡玩下面的語句:
[sample
Foreach(DataRow row in tabl
Foreach(DataColumn col in lumns)
……………
clsOraDb
Next
…………
Next
說到這
現在
針對[sample
Public Shared Function getOEMPN(ByVal psPN As String
Dim clsResult As New BSResult
Try
clsResult
Dim dtResult As New DataTable
Dim Sql As String = String
Dim clsOraDb As New clsOraClienDb
Dim strConn As String =
ConfigurationManager
clsOraDb
Sql =
Dim params() As racleParameter = {New OracleParameter(
If clsOraDb
Return clsResult
End If
If dtResult Is Nothing Then
Return clsResult
End If
If dtResult
OEMPN = dtResult
Else
OEMPN =
End If
clsResult
clsOraDb
Return clsResult
Catch ex As Exception
clsOraDb
clsResult
Return clsResult
Throw ex
End Try
End Function
注意上面的兩句代碼:clsOraDb
在異常處理的時候
(
(
針對[Sample
clsOraDb
Foreach(DataRow row in tabl
……………
…………
Next
當然還有另外一個做法
我們繼續用[Sample
Dim dtResult As New DataTable
誰會發現它被釋放
那麼怎麼解決這些問題呢?
(
Dim dtResult As New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
Catch ex As Exception
Throw ex
Finally
End Try
(
Dim dtResult As New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
……………
Catch ex As Exception
Throw ex
Finally
dtResult
DR
DS
End Try
有人習慣寫成下面這樣:
Dim dtResult As New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
dtResult
DR
DS
Catch ex As Exception
Throw ex
Finally
End Try
這不是也釋放了嗎?我想問的是
它們一定不能釋放
(
Foreach (DataRow row in tabl
……………
Dim DS new Dataset 記住此乃寫代碼之大忌;
Dim DT new Databable…
…………
Next
還有一種寫法
Dim DS new Dataset
Dim DT new Databable…
Foreach (DataRow row in tabl
DS=GetDatase
DT=GetDatatable……………
…………
Next
正確的寫法是
Dim DS new Dataset
Dim DT new Databable…
Try
Foreach (DataRow row in tabl
DS=nothing
DT=nothing
DS=GetDatase
DT=GetDatatable……………
…………
Next
Catch ex As Exception
Throw ex
Finally
DS
DT
End Try
另外
大家看下面的圖片
.NET編程免費提供,內容來源於互聯網,本文歸原作者所有。