|
| 1 | +/* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with The MIT |
| 6 | + * License (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * https://github.com/oracle/Oracle.NET/blob/master/LICENSE |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + *****************************************************************************/ |
| 19 | + |
| 20 | +using System; |
| 21 | +using Oracle.ManagedDataAccess.Client; |
| 22 | +using Oracle.ManagedDataAccess.Types; |
| 23 | +using System.Data; |
| 24 | + |
| 25 | +namespace GetStartedODPNETCore |
| 26 | +{ |
| 27 | + class Program |
| 28 | + { |
| 29 | + static void Main(string[] args) |
| 30 | + { |
| 31 | + |
| 32 | + //Demo: Basic ODP.NET Core application to connect, query, and return |
| 33 | + // results from an OracleDataReader to a console |
| 34 | + |
| 35 | + //Enter user name and password |
| 36 | + string conString = "User Id=hr;Password=<password>;" + |
| 37 | + |
| 38 | + //Connect to an Oracle DB with Easy Connect (Plus) or a net service name |
| 39 | + "Data Source=<Easy Connect (Plus) or net service name>;"; |
| 40 | + |
| 41 | + using (OracleConnection con = new OracleConnection(conString)) |
| 42 | + { |
| 43 | + using (OracleCommand cmd = con.CreateCommand()) |
| 44 | + { |
| 45 | + try |
| 46 | + { |
| 47 | + con.Open(); |
| 48 | + cmd.BindByName = true; |
| 49 | + |
| 50 | + //Use the command to display employee names from the EMPLOYEES table |
| 51 | + cmd.CommandText = "select first_name from employees where department_id = :id"; |
| 52 | + |
| 53 | + //Assign id to the department number 20 |
| 54 | + OracleParameter id = new OracleParameter("id", 20); |
| 55 | + cmd.Parameters.Add(id); |
| 56 | + |
| 57 | + //Execute the command and use DataReader to display the data |
| 58 | + OracleDataReader reader = cmd.ExecuteReader(); |
| 59 | + while (reader.Read()) |
| 60 | + { |
| 61 | + Console.WriteLine("Employee First Name: " + reader.GetString(0)); |
| 62 | + } |
| 63 | + |
| 64 | + id.Dispose(); |
| 65 | + reader.Dispose(); |
| 66 | + Console.WriteLine(); |
| 67 | + |
| 68 | + //Demo: Batch SQL and REF Cursors |
| 69 | + // Anonymous PL/SQL block embedded in code - executes in one DB round trip |
| 70 | + |
| 71 | + //Reset OracleCommand for use in next demo |
| 72 | + cmd.Parameters.Clear(); |
| 73 | + cmd.BindByName = false; |
| 74 | + |
| 75 | + cmd.CommandText = "DECLARE a NUMBER:= 20; " + |
| 76 | + "BEGIN " + |
| 77 | + "OPEN :1 for select first_name,department_id from employees where department_id = 10; " + |
| 78 | + "OPEN :2 for select first_name,department_id from employees where department_id = a; " + |
| 79 | + "OPEN :3 for select first_name,department_id from employees where department_id = 30; " + |
| 80 | + "END;"; |
| 81 | + |
| 82 | + cmd.CommandType = CommandType.Text; |
| 83 | + |
| 84 | + //ODP.NET has native Oracle data types, such as Oracle REF |
| 85 | + // Cursors, which can be mapped to .NET data types |
| 86 | + |
| 87 | + //Bind REF Cursor Parameters for each department |
| 88 | + //Select employees in department 10 |
| 89 | + OracleParameter p1 = cmd.Parameters.Add("refcursor1", |
| 90 | + OracleDbType.RefCursor); |
| 91 | + p1.Direction = ParameterDirection.Output; |
| 92 | + |
| 93 | + //Select employees in department 20 |
| 94 | + OracleParameter p2 = cmd.Parameters.Add("refcursor2", |
| 95 | + OracleDbType.RefCursor); |
| 96 | + p2.Direction = ParameterDirection.Output; |
| 97 | + |
| 98 | + //Select employees in department 30 |
| 99 | + OracleParameter p3 = cmd.Parameters.Add("refcursor3", |
| 100 | + OracleDbType.RefCursor); |
| 101 | + p3.Direction = ParameterDirection.Output; |
| 102 | + |
| 103 | + //Execute batched statement |
| 104 | + cmd.ExecuteNonQuery(); |
| 105 | + |
| 106 | + //Let's retrieve the three result sets with DataReaders |
| 107 | + OracleDataReader dr1 = |
| 108 | + ((OracleRefCursor)cmd.Parameters[0].Value).GetDataReader(); |
| 109 | + OracleDataReader dr2 = |
| 110 | + ((OracleRefCursor)cmd.Parameters[1].Value).GetDataReader(); |
| 111 | + OracleDataReader dr3 = |
| 112 | + ((OracleRefCursor)cmd.Parameters[2].Value).GetDataReader(); |
| 113 | + |
| 114 | + //Let's retrieve the results from the DataReaders |
| 115 | + while (dr1.Read()) |
| 116 | + { |
| 117 | + Console.WriteLine("Employee Name: " + dr1.GetString(0) + ", " + |
| 118 | + "Employee Dept:" + dr1.GetDecimal(1)); |
| 119 | + } |
| 120 | + Console.WriteLine(); |
| 121 | + |
| 122 | + while (dr2.Read()) |
| 123 | + { |
| 124 | + Console.WriteLine("Employee Name: " + dr2.GetString(0) + ", " + |
| 125 | + "Employee Dept:" + dr2.GetDecimal(1)); |
| 126 | + } |
| 127 | + Console.WriteLine(); |
| 128 | + |
| 129 | + while (dr3.Read()) |
| 130 | + { |
| 131 | + Console.WriteLine("Employee Name: " + dr3.GetString(0) + ", " + |
| 132 | + "Employee Dept:" + dr3.GetDecimal(1)); |
| 133 | + } |
| 134 | + |
| 135 | + //Clean up |
| 136 | + p1.Dispose(); |
| 137 | + p2.Dispose(); |
| 138 | + p3.Dispose(); |
| 139 | + dr1.Dispose(); |
| 140 | + dr2.Dispose(); |
| 141 | + dr3.Dispose(); |
| 142 | + |
| 143 | + Console.WriteLine("Press 'Enter' to continue"); |
| 144 | + } |
| 145 | + catch (Exception ex) |
| 146 | + { |
| 147 | + Console.WriteLine(ex.Message); |
| 148 | + } |
| 149 | + Console.ReadLine(); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments