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