1.VB.NET使ç¨OracleTransactionå¤çäºå¡
VB.NET使ç¨OracleTransactionå¤çäºå¡
æ°æ®åºäºå¡ç®ä»
æ°æ®åºäºå¡æ¯ç±ä¸ç» SQL è¯å¥ç»æçä¸ä¸ªé»è¾å·¥ä½åå ãæ¨å¯ä»¥æäºå¡çä½æ¯ä¸ç»ä¸å¯åç SQL è¯å¥ï¼è¿äºè¯å¥ä½ä¸ºä¸ä¸ªæ´ä½æ°¸ä¹ è®°å½å¨æ°æ®åºä¸æä¸å¹¶æ¤éãæ¯å¦å¨é¶è¡å¸æ·ä¹é´è½¬ç§»èµéï¼ä¸æ¡ UPDATEè¯å¥å°ä»ä¸ä¸ªå¸æ·çèµéæ»æ°ä¸åå»ä¸é¨åï¼å¦ä¸æ¡ UPDATEè¯å¥å°æèµéå å°å¦ä¸ä¸ªå¸æ·ä¸ãåæä½åå æä½å¿ é¡»æ°¸ä¹ è®°å½å¨æ°æ®åºä¸ï¼æè å¿ é¡»ä¸å¹¶æ¤é â å¦åå°æ失èµéãè¿ä¸ªç®åç示ä¾ä» 使ç¨äºä¸¤æ¡ UPDATE è¯å¥ï¼ä½ä¸ä¸ªæ´å®é çäºå¡å¯è½å å«è®¸å¤ INSERTãUPDATEå DELETE è¯å¥ã
è¦æ°¸ä¹ è®°å½ä¸ä¸ªäºå¡ä¸ç SQL è¯å¥çç»æï¼æ¨å¯ä»¥éè¿ COMMIT è¯å¥æ¥æ§è¡æ交ãè¦æ¤é SQL è¯å¥çç»æï¼æ¨å¯ä»¥ä½¿ç¨ ROLLBACK è¯å¥æ¥æ§è¡åæ»ï¼è¿ä¼æææçè¡é设为å®ä»¬åæ¥çç¶æãåªè¦æ¨äºå 没æä¸æ°æ®åºæå¼ï¼åæ¨å¨æ§è¡åæ»ä¹åæåçä»»ä½ä¿®æ¹é½å°è¢«æ¤éãæ¨è¿å¯ä»¥è®¾ç½®ä¸ä¸ªä¿åç¹ï¼ä»¥ä¾¿å°äºå¡åæ»è³è¯¥ç¹å®çç¹ï¼åæ¶ä¿æäºå¡ä¸çå ¶ä»è¯å¥åå°ä¸å¨ã
使ç¨æ°æ®åºäºå¡(主è¦é对)
æ¨å¯ä»¥ä½¿ç¨ OracleTransaction ç±»çä¸ä¸ªå¯¹è±¡æ¥è¡¨ç¤ºä¸ä¸ªäºå¡ãOracleTransaction ç±»å å«å¤ä¸ªå±æ§ï¼å ¶ä¸ç两个为 Connectionï¼æå®ä¸äºå¡å ³èçæ°æ®åºè¿æ¥ï¼å IsolationLevelï¼æå®äºå¡é离级å«ï¼
Connectionï¼æå®ä¸è¯¥äºå¡å ³èçOracleConnection对象ï¼
IsolationLevelï¼æå®è¯¥äºå¡çIsolationLevelï¼æ举类åï¼ç¨äºå¯¹äºç©çéå®ï¼åå¼æChaosãReadCommitedãReadUncommitedãRepeatableReadãSerializableãUnspecifiedã
åºç¨ç¨åºéè¿é对OracleConnection对象è°ç¨ BeginTransaction æ¥å建OracleTransaction对象ã对OracleTransaction对象æ§è¡ä¸è¯¥äºå¡å ³èçææåç»æä½ï¼ä¾å¦æ交æä¸æ¢è¯¥äºå¡ï¼ã
Commit:æ交SQLæ°æ®åºäºå¡ï¼
Rollback :ä»æèµ·ç¶æåæ»äºå¡ï¼
æ¨è¿å¯ä»¥ä½¿ç¨ Save() å¨äºå¡ä¸è®¾ç½®ä¸ä¸ªä¿åç¹ã
ä¸é¢ç示ä¾å建ä¸ä¸ª OracleConnection åä¸ä¸ª OracleTransactionãå®è¿æ¼ç¤ºäºå¦ä½ä½¿ç¨ BeginTransactionãCommit å Rollback æ¹æ³ãï¼è¿æ¯MSDNéçèä¾ï¼
éè¦æ³¨æçæ¯ï¼è¿äºæä½éè¦å¼å ¥å½å空é´: Oracle.DataAcess.Client
Oracle.DataAccess.Client å½å空é´æ¯
çä¸é¨åï¼å®å å«è®¸å¤ç±»ï¼å ¶ä¸æ OracleConnectionãOracleCommand å OracleTransactionã示ä¾ç¨åºå°±ç¨å°äºè¿äºç±»ã
äºå¡æä½
1Public Sub RunOracleTransaction()Sub RunOracleTransaction(myConnString As String)
2 Dim myConnection As New OracleConnection(myConnString)
3 myConnection.Open()
4
5 Dim myCommand As OracleCommand = myConnection.CreateCommand()
6 Dim myTrans As OracleTransaction
7
8 ' Start a local transaction
9 myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted)
' Assign transaction object for a pending local transaction
myCommand.Transaction = myTrans
Try
myCommand.CommandText = "INSERT INTO Dept (DeptNo,电商AI软件源码 Dname, Loc) values (, 'TECHNOLOGY', 'DENVER')"
myCommand.ExecuteNonQuery()
myCommand.CommandText = "INSERT INTO Dept (DeptNo, Dname, Loc) values (, 'ENGINEERING', 'KANSAS CITY')"
myCommand.ExecuteNonQuery()
myTrans.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
myTrans.Rollback()
Console.WriteLine(e.ToString())
Console.WriteLine("Neither record was written to database.")
Finally
myConnection.Close()
End Try
End Sub
å¨ .NET ç¨åºä¸è®¾ç½®äºå¡ä¿åç¹
æ£å¦æ¬æåé¢ææå°çé£æ ·ï¼æ¨å¯ä»¥è®¾ç½®ä¸ä¸ªä¿åç¹ï¼ä»¥ä¾¿å°äºå¡åæ»è³è¯¥ç¹å®çç¹ï¼åæ¶ä¿æäºå¡ä¸çå ¶ä»è¯å¥åå°ä¸å¨ãæ¨å¯ä»¥ä½¿ç¨ OracleTransaction ç±»ç Save() æ¹æ³å¨äºå¡ä¸è®¾ç½®ä¿åç¹ã
å¦ææ¨æä¸ä¸ªé常é¿çäºå¡å¹¶ä¸å¸æè½å¤ä» åæ»å°æ个ç¹å®çæ¶é´ç¹ï¼é£ä¹æ¨å¯è½è¦ä½¿ç¨ä¿åç¹ãä¾å¦ï¼æ¨å¯è½æ³å¯¹ 个产ååä¸äºæ´æ¹ï¼ç¶å设置ä¸ä¸ªä¿åç¹ï¼ç¶ååå¯¹å¦ ä¸ªäº§ååæ´æ¹ï¼å¦ææ¨å¨è¿è¡ç¬¬äºæ¹æ´æ¹æ¶åºç°äºé误ï¼é£ä¹æ¨å¯ä»¥åæ»è³ä¿åç¹ï¼ä½¿æ¨ç第ä¸æ¹æ´æ¹åå°ä¸å¨ã
使ç¨OracleTransaction对象éè¦æ³¨æçå ç¹ï¼
1)ä½ éè¦å¨ä½ æ´ä¸ªäºå¡æ§è¡ä¸åªè½æå¯ä¸OracleConnection ï¼OracleCommandï¼
OracleTransactionï¼ä¹å°±æ¯è¯´å¦æä½ äºå¡å¤çè¿ç¨ä¸å¦æéè¦ä¸æ°æ®åºçæä½é½åªè½å¨è¿å¯ä¸çCommandä¸æ§è¡,类似äº:
imgCommand.CommandText = sSQL
imgCommand.ExecuteNonQuery()æå ¶ä»æä½
è¥ä½ æ°å»ºä¸ä¸ªè¿æ¥æ§è¡å ¶ä»æ°æ®åºæä½çè¯ï¼æ´ä¸ªäºå¡è¿ç¨å°±ä¼æåºå¼å¸¸
2)å¦æä½ éè¦å¨ä½ SQLè¯å¥ä¸å å ¥åæ°ï¼åä½ å¿ é¡»å¨ä½ æ§è¡å®æ交æç¸å ³æ°æ®åºæä½ä¹åå°å ¶Commandçåæ°æ¸ 空,ä¸è¾¹ä¸¾ä¸ä¸ªå®é ç项ç®éçäºå¡å½æ°ï¼
äºå¡å½æ°
1 ''' summary
2 ''' ä¿åçç¹ææ¬æ件信æ¯å°æ°æ®åº
3 ''' /summary
4 Private Function SaveTextFile()Function SaveTextFile() As Boolean
5 Dim sSQl As String
6 sSQl = "select type_id from sys_file_type where file_extname='TXT'"
7 Try
8 imgCommand.CommandText = sSQl
9 Dim typeID As Int = Convert.ToInt(imgCommand.ExecuteScalar()) 'æ件类å
'读åææ¬ä¿¡æ¯
Dim Textblob() As Byte = GetText()
sSQl = "insert into t_watch_textcontent(image_id,text_content,type_id) values(:imageid,:textcontent,:typeid)"
'å¢æ·»SQLåæ°
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
'å ³é®æ¯è¿éï¼éè¦ä½ æå¨æ¸ é¤åæ°
imgCommand.Parameters.Clear()
End If
Catch ex As Exception
Me.ExceptionMessage = ex
bResult = False
End Try
Return bResult
End Function