翻譯|使用教程|編輯:李爽夏|2019-01-21 09:31:59.000|閱讀 241 次
概述:本教程介紹如何使用OracleCommand、OracleDataReader和OracleDataTable組件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
【下載dotConnect for Oracle最新版本】
dotConnect for Oracle(原名OraDirect.NET)建立在ADO.NET技術(shù)上,為基于Oracle數(shù)據(jù)庫(kù)的應(yīng)用程序提供完整的解決方案。它為設(shè)計(jì)應(yīng)用程序結(jié)構(gòu)帶來(lái)了新的方法,提高工作效率,使數(shù)據(jù)庫(kù)應(yīng)用程序的開發(fā)更簡(jiǎn)便。
檢索和修改數(shù)據(jù)
本教程介紹如何使用OracleCommand、OracleDataReader和OracleDataTable組件。
本教程假設(shè)您知道如何連接到服務(wù)器,如何在服務(wù)器上創(chuàng)建必要的對(duì)象,以及如何將數(shù)據(jù)插入到創(chuàng)建的表中。
請(qǐng)注意,如果您不使用設(shè)計(jì)時(shí)(特別是,如果您不從工具箱放置在設(shè)計(jì)器或OracleConnection組件上),則必須手動(dòng)嵌入許可證信息。
如我們所知,任何數(shù)據(jù)庫(kù)應(yīng)用程序的原始功能都是建立到數(shù)據(jù)源的連接,并處理其中包含的數(shù)據(jù)。ADO.NET的.NET框架數(shù)據(jù)提供程序充當(dāng)應(yīng)用程序和數(shù)據(jù)源之間的橋梁,允許您執(zhí)行命令以及使用DataReader或DataAdapter檢索數(shù)據(jù)。更新數(shù)據(jù)涉及到使用命令和數(shù)據(jù)適配器對(duì)象;它還可能涉及使用事務(wù)。
讓我們進(jìn)行一些分類,以便更好地理解ADO.NET模型。使用數(shù)據(jù)有兩種方法:連接和斷開連接的模型。您可以使用連接模型的類來(lái)建立連接和設(shè)置事務(wù)、獲取數(shù)據(jù)和更新數(shù)據(jù)源。這些類直接與數(shù)據(jù)庫(kù)交互:oracleProviderFactory、oracleConnection、oracleTransaction、oracleDataAdapter、oracleCommand、oracleParameter和oracleDataReader。
這些對(duì)象表示ADO.NET的斷開連接的模型,不會(huì)立即與數(shù)據(jù)源進(jìn)行互操作。這些類提供了脫機(jī)處理數(shù)據(jù)存儲(chǔ)的能力:數(shù)據(jù)集、數(shù)據(jù)表、數(shù)據(jù)列、數(shù)據(jù)行、約束、數(shù)據(jù)關(guān)系、數(shù)據(jù)視圖和數(shù)據(jù)行視圖。
我們將在示例中使用兩個(gè)模型中的類。
本教程的目標(biāo)是從table dept檢索和更新數(shù)據(jù)(適當(dāng)?shù)腄DL/DML腳本位于\Program Files\Devart\dotConnect\Oracle\Samples\tables.sql——用于dotConnect for Oracle的默認(rèn)路徑)。
在本示例中,我們使用OracleCommand和 OracleDataReader來(lái)檢索和操作數(shù)據(jù)。
using Devart.Data.Oracle; ... class Program { void PrintDept(OracleConnection connection) { OracleCommand command = connection.CreateCommand(); command.CommandText = "select * from dept"; // Call the Close method when you are finished using the OracleDataReader // to use the associated OracleConnection for any other purpose. // Or put the reader in the using block to call Close implicitly. using (OracleDataReader reader = command.ExecuteReader()) { // printing the column names for (int i = 0; i < reader.FieldCount; i++) Console.Write(reader.GetName(i).ToString() + "\t"); Console.Write(Environment.NewLine); // Always call Read before accesing data while (reader.Read()) { // printing the table content for (int i = 0; i < reader.FieldCount; i++) Console.Write(reader.GetValue(i).ToString() + "\t"); Console.Write(Environment.NewLine); } } } void ModifyDept(OracleConnection connection) { OracleCommand command = connection.CreateCommand(); command.CommandText = "UPDATE DEPT SET LOC='VEGAS' WHERE DEPTNO > 20"; // return value of ExecuteNonQuery (i) is the number of rows affected by the command int i = command.ExecuteNonQuery(); Console.WriteLine(Environment.NewLine + "Rows in DEPT updated: {0}", i + Environment.NewLine); } static void Main(string[] args) { using (OracleConnection conn = new OracleConnection("User Id=Scott;Password=tiger;Data Source=Ora;")) { try { conn.Open(); Program program = new Program(); // printing out the Dept table to console program.PrintDept(conn); // updating records in Dept program.ModifyDept(conn); // printing out the Dept table to console program.PrintDept(conn); } catch (OracleException ex) { Console.WriteLine("Exception occurs: {0}", ex.Message); } finally { Console.ReadLine(); } } } }
Imports Devart.Data.Oracle ... Module Module1 Sub PrintDept(ByVal connection As OracleConnection) Dim command As OracleCommand = connection.CreateCommand() command.CommandText = "select * from dept" ' Call the Close method when you are finished using the OracleDataReader ' to use the associated OracleConnection for any other purpose. ' Or put the reader in the using block to call Close implicitly. Using reader As OracleDataReader = command.ExecuteReader() ' printing the column names For i As Integer = 0 To reader.FieldCount - 1 Console.Write(reader.GetName(i).ToString() & VbCrlf) Next i Console.Write(Environment.NewLine) ' Always call Read before accesing data While reader.Read() ' printing the table content For i As Integer = 0 To reader.FieldCount - 1 Console.Write(reader.GetValue(i).ToString() & VbCrlf) Next Console.Write(Environment.NewLine) End While End Using End Sub Sub ModifyDept(ByVal connection As OracleConnection) Dim command As OracleCommand = connection.CreateCommand() command.CommandText = "UPDATE DEPT SET LOC='VEGAS' WHERE DEPTNO > 20" ' return value of ExecuteNonQuery (i) is the number of rows affected by the command Dim i As Integer = command.ExecuteNonQuery() Console.WriteLine(Environment.NewLine & "Rows in DEPT updated: {0}", i & Environment.NewLine) End Sub Sub Main() Using conn _ As New OracleConnection("User Id=Scott;Password=tiger;Data Source=Ora;") Try conn.Open() ' printing out the Dept table to console Module1.PrintDept(conn) ' updating records in Dept Module1.ModifyDept(conn) ' printing out the Dept table to console Module1.PrintDept(conn) Catch ex As OracleException Console.WriteLine("Exception occurs: {0}", ex.Message) Finally Console.ReadLine() End Try End Using End Sub End Module
使用數(shù)據(jù)表和數(shù)據(jù)集的傳統(tǒng)方法假定連續(xù)創(chuàng)建和初始化連接、命令、數(shù)據(jù)適配器和commandbuilder對(duì)象。Devart OracleDataTable和OracleDataset具有高級(jí)功能,可以更輕松地處理數(shù)據(jù)。更重要的是,使用我們的組件,您可以在設(shè)計(jì)時(shí)檢索和操作數(shù)據(jù)。
下面是一個(gè)演示OracleDataTable用法的小示例。
public void UseDataTable() { OracleDataTable myDataTable = new OracleDataTable("SELECT * FROM Dept", "User Id=Scott;Password=tiger;Data Source=Ora;"); try { // FetchAll=true means to retrieve data from server entirely when DataTable is opened. // By default, FetchAll is set to false - only minimal quantity of rows is requested at once, // which leads to better initial response time and less network traffic. myDataTable.FetchAll = true; // populating DataTable with data from data source myDataTable.Active = true; // modifying the third record myDataTable.Rows[3]["DName"] = "Researches"; // Update method executes the appropriate commands (delete, insert, or update) in the data source. Console.WriteLine(myDataTable.Update() + " rows updated."); // printing the DataTable content foreach (DataRow myRow in myDataTable.Rows) { foreach (DataColumn myCol in myDataTable.Columns) { Console.Write(myRow[myCol] + "\t"); } Console.WriteLine(); } } finally { //Active=false does not clear the data, but frees the resources allocated on the server, if any. myDataTable.Active = false; } }
Public Sub UseDataTable() Dim myDataTable As OracleDataTable _ As New OracleDataTable("SELECT * FROM Dept", "User Id=Scott;Password=tiger;Data Source=Ora;") Try ' FetchAll=true means to retrieve data from server entirely when DataTable is opened. ' By default, FetchAll is set to false - only minimal quantity of rows is requested at once, ' which leads to better initial response time and less network traffic. myDataTable.FetchAll = True ' populating DataTable with data from data source myDataTable.Active = True ' modifying the third record myDataTable.Rows(3)("DName") = "Researches" ' Update method executes the appropriate commands (delete, insert, or update) in the data source. Console.WriteLine(myDataTable.Update() & " rows updated.") Dim myRow As DataRow Dim myCol As DataColumn ' printing the DataTable content For Each myRow In myDataTable.Rows For Each myCol In myDataTable.Columns Console.Write(myRow(myCol) & VbCrlf) Next myCol Console.WriteLine() Next myRow Finally ' Active=false does not clear the data, but frees the resources allocated on the server, if any. myDataTable.Active = False End Try End Sub
使用Devart數(shù)據(jù)集向?qū)Э梢暂p松創(chuàng)建OracleDataset,并使用Devart數(shù)據(jù)集管理器進(jìn)行可視化管理。
本教程只介紹處理數(shù)據(jù)的基本方法。此外,還可以利用存儲(chǔ)過(guò)程、類型化數(shù)據(jù)集和ORM解決方案。Dotconnect for Oracle支持LinqConnect和實(shí)體框架ORM技術(shù),用于在關(guān)系數(shù)據(jù)庫(kù)中的不兼容類型系統(tǒng)和面向?qū)ο缶幊陶Z(yǔ)言之間轉(zhuǎn)換數(shù)據(jù)。這些技術(shù)允許您減少面向數(shù)據(jù)應(yīng)用程序所需的代碼和維護(hù)量。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn