VB.NET使用OracleTransaction對象深入剖析
VB.NET還是比較常用的,于是我研究了一下VB.NET使用OracleTransaction對象,在這里拿出來和大家分享一下,希望對大家有用。
VB.NET使用OracleTransaction對象需要注意的幾點(diǎn):
1)你需要在你整個(gè)事務(wù)執(zhí)行中只能有***OracleConnection,也就是說如果你事務(wù)處理過程中如果需要與數(shù)據(jù)庫的操作都只能在這***的Command中執(zhí)行,類似于:
- imgCommand.CommandText = sSQL
- imgCommand.ExecuteNonQuery()或其他操作
若你新建一個(gè)連接執(zhí)行其他數(shù)據(jù)庫操作的話,整個(gè)事務(wù)過程就會拋出異常
2)如果你需要在你SQL語句中加入?yún)?shù),則你必須在你執(zhí)行完提交或相關(guān)數(shù)據(jù)庫操作之后將其Command的參數(shù)清空,下邊舉一個(gè)實(shí)際的項(xiàng)目里的事務(wù)函數(shù):
- ''' <summary>
- ''' 保存熱點(diǎn)文本文件信息到數(shù)據(jù)庫
- ''' </summary>
- Private Function SaveTextFile()Function SaveTextFile() As Boolean
- Dim sSQl As String
- sSQl = "select type_id from sys_file_type where file_extname='TXT'"
- Try
- imgCommand.CommandText = sSQl
- Dim typeID As Int32 = Convert.ToInt32(imgCommand.ExecuteScalar()) '文件類型
- '讀取文本信息
- Dim Textblob() As Byte = GetText()
- sSQl = "insert into t_watch_textcontent(image_id,text_content,type_id)
values(:imageid,:textcontent,:typeid)"- '增添SQL參數(shù)
- Dim Param As OracleClient.OracleParameter
- Param = New OracleClient.OracleParameter("imageid", sNewImageID)
- imgCommand.Parameters.Add(Param)
- Param = New OracleClient.OracleParameter("textcontent", Textblob)
- imgCommand.Parameters.Add(Param)
- Param = New OracleClient.OracleParameter("typeid", typeID)
- imgCommand.Parameters.Add(Param)
- '提交信息
- imgCommand.CommandText = sSQl
- If imgCommand.ExecuteNonQuery() > 0 Then
- bResult = True
- '關(guān)鍵是這里,需要你手動(dòng)清除參數(shù)
- imgCommand.Parameters.Clear()
- End If
- Catch ex As Exception
- Me.ExceptionMessage = ex
- bResult = False
- End Try
- Return bResult
- End Function
以上介紹VB.NET使用OracleTransaction對象。
【編輯推薦】