原创|使用教程|编辑:郝浩|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@capbkgr.cn
文章转载自:慧都控件