原創|使用教程|編輯:郝浩|2013-04-17 17:30:34.000|閱讀 590 次
概述:如何在dotConnect for Oracle中檢索和修改數據?本文將會講到如何使OracleCommand 、 OracleDataReader、OracleDataTable組件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
如何在dotConnect for Oracle中檢索和修改數據?本文將會講到如何使用OracleCommand 、 OracleDataReader、OracleDataTable組件。
如果說你已經連接到Oracle服務器,在以前的文章中已經提到過如何在服務器上創建對象。
如果說你不使用設計時,就是說你沒有將來自工具箱中的OracleConnection組件放在設計器上的話,你就必須要手動的完成許可信息。
檢索和更新的數據使用的連接模型
在下面的例子中,將會使用OracleCommand和OracleDataReader來檢索和操作數據。
[C#]
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;Server=ORA;Persist Security Info=True;")) { 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(); } } } }
[Visual Basic]
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;Server=ORA;Persist Security Info=True;") 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
使用Disconnected模式檢索和更新數據
對于數據表格和數據集有一個傳統的方法,來創建和初始化連接、命令、數據更新和命令創建器。下面是演示了使用oracledatatable的一個例子:
[C#]
public void UseDataTable() { OracleDataTable myDataTable = new OracleDataTable("SELECT * FROM Dept", "User Id=scott;Password=tiger;Server=ORA;Persist Security Info=True;"); 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; } }
[Visual Basic]
Public Sub UseDataTable() Dim myDataTable As OracleDataTable _ As New OracleDataTable("SELECT * FROM Dept", "User Id=scott;Password=tiger;Server=ORA;Persist Security Info=True;") 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
oracledataset可以輕松創建數據集向導,以及使用Devart數據集管理器實現可視化的管理。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件