oracle事務(wù)庫怎么用,oracle數(shù)據(jù)庫常用操作

.NET中如何使用Oracle數(shù)據(jù)庫事務(wù)(2)

在C# 和 VB.NET 中使用數(shù)據(jù)庫事務(wù)您可以使用 OracleTransaction 類的一個對象來表示一個事務(wù)。OracleTransaction 類包含多個屬性,其中的兩個為 Connection(指定與事務(wù)關(guān)聯(lián)的數(shù)據(jù)庫連接)和 IsolationLevel(指定事務(wù)隔離級別);本文稍后將向您介紹更多有關(guān)事務(wù)隔離級別的內(nèi)容。 OracleTransaction 類包含許多操控事務(wù)的方法。您可以使用 Commit() 方法永久提交 SQL 語句,并可以使用 Rollback() 撤銷這些語句。您還可以使用 Save() 在事務(wù)中設(shè)置一個保存點。 我現(xiàn)在將帶著您逐步完成兩個示例程序 ― 一個用 C# 編寫 (TransExample1.cs),另一個用 VB.NET 編寫 (TransExample1.vb)。這些程序演示了如何執(zhí)行一個包含了兩條 INSERT 語句的事務(wù)。第一條 INSERT 語句將在表 product_types 中添加一行,第二條將在表 products 中添加一行。 導(dǎo)入命名空間以下C# 程序語句指定在程序中使用 System 和 Oracle.DataAcess.Client 命名空間: using System; using Oracle.DataAccess.Client;下面是等價的 VB.NET 語句: Imports System Imports Oracle.DataAccess.ClientOracle.DataAccess.Client 命名空間是 ODP.NET 的一部分,它包含許多類,其中有OracleConnection、OracleCommand 和 OracleTransaction。示例程序用到了這些類。 第1 步 創(chuàng)建一個 OracleConnection 對象連接到 Oracle 數(shù)據(jù)庫,然后打開該連接。 在C# 中: OracleConnection myOracleConnection = new OracleConnection( "User Id=store;Password=store;Data Source=ORCL" ); myOracleConnection.Open();在VB.NET 中: Dim myOracleConnection As New OracleConnection( _ "User Id=store;Password=store;Data Source=ORCL") myOracleConnection.Open()User Id 和 Password 屬性指定了您所要連接到的模式的數(shù)據(jù)庫用戶和口令。Data Source 屬性指定了數(shù)據(jù)庫的 Oracle Net 服務(wù)名稱;初始數(shù)據(jù)庫的默認服務(wù)名稱為 ORCL。如果您使用的不是初始數(shù)據(jù)庫,或者您的服務(wù)名稱不同,那么您需要在程序中修改 Data Source 屬性的設(shè)置。 第2 步 創(chuàng)建一個 OracleTransaction 對象,然后調(diào)用 OracleConnection 對象的 BeginTransaction() 方法啟動事務(wù)。 在C# 中: OracleTransaction myOracleTransaction = myOracleConnection.BeginTransaction(); In VB.NET: Dim myOracleTransaction As OracleTransaction = _ myOracleConnection.BeginTransaction()第3 步 創(chuàng)建一個 OracleCommand 對象,用于存儲 SQL 語句。 在C# 中: OracleCommand myOracleCommand = myOracleConnection.CreateCommand();在VB.NET 中: Dim myOracleCommand As OracleCommand = myOracleConnection.CreateCommand因為OracleCommand 對象使用 OracleConnection 對象的 CreateCommand() 方法創(chuàng)建的,所以它自動使用在第 2 步中為 OracleConnection 對象設(shè)置的事務(wù)。 第4 步 將OracleCommand 對象的 CommandText 屬性設(shè)為向表 product_types 中添加一行的第一條 INSERT 語句。 在C# 中: myOracleCommand.CommandText = "INSERT INTO product_types (" + " product_type_id, name" + ") VALUES (" + " 3, 'Magazine'" + ")";在VB.NET 中: myOracleCommand.CommandText = _ "INSERT INTO product_types (" _ " product_type_id, name" _ ") VALUES (" _ " 3, 'Magazine'" _ ")"

創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為尚志等服務(wù)建站,尚志等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為尚志企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

求問在.NET中如何使用Oracle數(shù)據(jù)庫事務(wù) (5)

您可以使用 OracleTransaction 類的 Save() 方法在事務(wù)中設(shè)置保存點。 如果您有一個非常長的事務(wù)并且希望能夠僅回滾到某個特定的時間點,那么您可能要使用保存點。例如,您可能想對 10 個產(chǎn)品做一些更改,然后設(shè)置一個保存點,然后再對另 10 個產(chǎn)品做更改;如果您在進行第二批更改時出現(xiàn)了錯誤,那么您可以回滾至保存點,使您的第一批更改原封不動。 我將帶您逐步完成演示如何使用保存點的 C# (TransExample2.cs) 示例程序和 VB.NET (TransExample2.vb) 示例程序中的相關(guān)新步驟。這些程序向表 products 中添加一行,設(shè)置一個保存點,向表 products 中添加另一行,回滾至保存點,然后從表 products 中讀取這些行。在回滾至保存點后,只有添加到表 products 中的第一行保留了下來:第二行將已被刪除。 第1 到第 3 步與“在 C# 和 VB.NET 中使用數(shù)據(jù)庫事務(wù)”部分中所示的步驟相同,因此在這里將其省略。 第4 步 向表products 中添加一行,該行的產(chǎn)品 ID 為 6。 在C# 中: myOracleCommand.CommandText = "INSERT INTO products (" + " product_id, product_type_id, name, description, price" + ") VALUES (" + " 6, 2, 'Man from Another World', ' Man from Venus lands on Earth', 24.99" + ")"; myOracleCommand.ExecuteNonQuery();在VB.NET 中: myOracleCommand.CommandText = _ "INSERT INTO products (" _ " product_id, product_type_id, name, description, price" _ ") VALUES (" _ " 6, 2, 'Man from Another World', 'Man from Venus lands on Earth', 24.99" _ ")" myOracleCommand.ExecuteNonQuery()第5 步 使用OracleTransaction 的 Save() 方法設(shè)置一個名為 SaveProduct 的保存點。 在C# 中: myOracleTransaction.Save("SaveProduct");在VB.NET 中: myOracleTransaction.Save("SaveProduct")第6 步 向表products 中添加另一行,該行的產(chǎn)品 ID 為 7。 在C# 中: myOracleCommand.CommandText = "INSERT INTO products (" + " product_id, product_type_id, name, description, price" + ") VALUES (" + " 7, 2, 'Z-Files', 'Mysterious stories', 14.99" + ")"; myOracleCommand.ExecuteNonQuery();在VB.NET 中: myOracleCommand.CommandText = _ "INSERT INTO products (" _ " product_id, product_type_id, name, description, price" _ ") VALUES (" _ " 7, 2, 'Z-Files', 'Mysterious stories', 14.99" _ ")" myOracleCommand.ExecuteNonQuery()第7 步 回滾到先前在第 5 步中設(shè)置的 SaveProduct 保存點。 在C# 中: myOracleTransaction.Rollback("SaveProduct");在VB.NET 中: myOracleTransaction.Rollback("SaveProduct")完成回滾后,在第 6 步中添加的第二行已被刪除,而在第 4 步中添加的第一行保留了下來。 TransExample2.cs 和 TransExample2.vb 中剩下的步驟顯示表 products 的內(nèi)容,回滾整個事務(wù)并從數(shù)據(jù)庫斷開。 用于Microsoft Transaction Server 的 Oracle 事務(wù)服務(wù)的快速說明。Microsoft Transaction Server 是一個運行在互聯(lián)網(wǎng)或網(wǎng)絡(luò)服務(wù)器上的專有事務(wù)處理系統(tǒng)。Microsoft Transaction Server 為客戶端計算機部署和管理應(yīng)用程序和數(shù)據(jù)庫事務(wù)請求。 Microsoft Transaction Server 是以服務(wù)器為中心的三層體系結(jié)構(gòu)模型的一個組件。這種方法實現(xiàn)了將應(yīng)用程序的表示、業(yè)務(wù)邏輯和數(shù)據(jù)元素清晰地分布到在一個網(wǎng)絡(luò)中連接的不同計算機上。無需專門集成,您就可以在與 Oracle 數(shù)據(jù)庫服務(wù)器 8.0.6 版或更高版本連接的 Microsoft Transaction Server 中部署一個組件,但首先您必須安裝 Oracle Services for Microsoft Transaction Server。 結(jié)論在本文中,您系統(tǒng)學(xué)習(xí)了在 .NET 程序中使用數(shù)據(jù)庫事務(wù)。 (T004)

Oracle中事務(wù)怎么使用 ,什么時候使用事物

事務(wù)是為了保證數(shù)據(jù)的完整性而設(shè)置的,在使用事務(wù)時,如果你是使用程序進行控制的話,一般都會自動提交事務(wù)的,需要手動進行事務(wù)的操作的是在進行批處理的時候,為了保證數(shù)據(jù)要么一起成功要么一起失敗,在開始數(shù)據(jù)的插入或者刪除之前,要把事務(wù)的自動提交改為false,然后才能執(zhí)行相關(guān)的SQL語句,當(dāng)SQL無異常全部執(zhí)行完以后,再手動提交就可以了

名稱欄目:oracle事務(wù)庫怎么用,oracle數(shù)據(jù)庫常用操作
鏈接URL:http://muchs.cn/article48/hcjiep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、做網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、標簽優(yōu)化、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站