1
+ // See https://aka.ms/new-console-template for more information
2
+ using System . Text ;
3
+ using Oracle . ManagedDataAccess . Client ;
4
+
5
+ internal class Program
6
+ {
7
+
8
+ public static OracleConnection createDBConnection ( string dbUsername ,
9
+ string dbPassword ,
10
+ string dbURL )
11
+ {
12
+ Console . WriteLine ( "\n Connecting to the database:" ) ;
13
+ Console . WriteLine ( "Database URL : " + dbURL ) ;
14
+ Console . WriteLine ( "Database Username : " + dbUsername ) ;
15
+ OracleConnection dbConnection = new OracleConnection ( "User Id=" + dbUsername + ";" +
16
+ "Password=" + dbPassword + ";" +
17
+ "Data Source=" + dbURL ) ;
18
+ dbConnection . Open ( ) ;
19
+ Console . WriteLine ( "\n Connected" ) ;
20
+ Console . WriteLine ( "---------------" ) ;
21
+ return dbConnection ;
22
+ }
23
+
24
+ public static void basicJDBCTest ( OracleConnection dbConnection )
25
+ {
26
+ Console . WriteLine ( "\n Executing a SELECT statement" ) ;
27
+ OracleCommand sqlCmd = dbConnection . CreateCommand ( ) ;
28
+ sqlCmd . CommandText = "SELECT LAST_NAME, FIRST_NAME "
29
+ + "FROM EMPLOYEES "
30
+ + "ORDER BY LAST_NAME" ;
31
+ OracleDataReader sqlReader = sqlCmd . ExecuteReader ( ) ;
32
+ Console . WriteLine ( "\n Query executed. Printing out results" ) ;
33
+ while ( sqlReader . Read ( ) )
34
+ {
35
+ // result set processing, example
36
+ Console . WriteLine ( sqlReader . GetString ( 0 ) + " " + sqlReader . GetString ( 1 ) ) ;
37
+ }
38
+ Console . WriteLine ( "\n Results printed out. Executing INSERT statements in a tranaction context" ) ;
39
+ OracleTransaction sqlTxn = dbConnection . BeginTransaction ( ) ;
40
+ sqlCmd . CommandText = "INSERT INTO EMPLOYEES_COPY "
41
+ + "SELECT * FROM EMPLOYEES "
42
+ + "WHERE DEPARTMENT_ID = 20" ;
43
+ sqlCmd . ExecuteNonQuery ( ) ;
44
+ sqlCmd . CommandText = "INSERT INTO DEPARTMENTS_COPY "
45
+ + "SELECT * FROM DEPARTMENTS "
46
+ + "WHERE DEPARTMENT_ID = 20" ;
47
+ sqlCmd . ExecuteNonQuery ( ) ;
48
+
49
+ Console . WriteLine ( "\n DML statement executed. Commiting transaction" ) ;
50
+ sqlTxn . Commit ( ) ;
51
+ Console . WriteLine ( "\n Transaction commited." ) ;
52
+ Console . WriteLine ( "----------------------------" ) ;
53
+ }
54
+
55
+ public static void basicAQTest ( OracleConnection dbConnection ,
56
+ string Message )
57
+ {
58
+ Console . WriteLine ( "\n Starting to demonstrate AQ/TEQ feature of Oracle Database" ) ;
59
+ OracleTransaction sqlTxn = dbConnection . BeginTransaction ( ) ;
60
+ OracleCommand enqueueSampleMessage = dbConnection . CreateCommand ( ) ;
61
+ enqueueSampleMessage . CommandText = "sample_enqueue" ;
62
+ enqueueSampleMessage . CommandType = System . Data . CommandType . StoredProcedure ;
63
+
64
+ OracleCommand dequeueSampleMessage = dbConnection . CreateCommand ( ) ;
65
+ dequeueSampleMessage . CommandText = "sample_dequeue" ;
66
+ dequeueSampleMessage . CommandType = System . Data . CommandType . StoredProcedure ;
67
+
68
+ OracleParameter inputMessage = new OracleParameter ( "p_message" , OracleDbType . Varchar2 , 2000 ) ;
69
+ inputMessage . Direction = System . Data . ParameterDirection . Input ;
70
+ inputMessage . Value = Message ;
71
+
72
+ OracleParameter outputMessage = new OracleParameter ( "p_message" , OracleDbType . Varchar2 , 2000 ) ;
73
+ outputMessage . Direction = System . Data . ParameterDirection . Output ;
74
+
75
+
76
+ enqueueSampleMessage . Parameters . Add ( inputMessage ) ;
77
+
78
+ dequeueSampleMessage . Parameters . Add ( outputMessage ) ;
79
+
80
+ enqueueSampleMessage . ExecuteNonQuery ( ) ;
81
+ dequeueSampleMessage . ExecuteNonQuery ( ) ;
82
+
83
+ Console . WriteLine ( outputMessage . Value ) ;
84
+
85
+ sqlTxn . Commit ( ) ;
86
+
87
+ Console . WriteLine ( "Enqueueing/Dequeueing sample message completed succesfully." ) ;
88
+ }
89
+
90
+ public static void closeDBConnection ( OracleConnection dbConnection )
91
+ {
92
+ Console . WriteLine ( "\n Closing DB connection." ) ;
93
+ dbConnection . Close ( ) ;
94
+ Console . WriteLine ( "Connection closed." ) ;
95
+ Console . WriteLine ( "----------------------------" ) ;
96
+ }
97
+
98
+ private static void nativeAQTest ( OracleConnection dbConnection )
99
+ {
100
+ OracleTransaction txn = dbConnection . BeginTransaction ( ) ;
101
+ OracleAQQueue sample_message_queue = new OracleAQQueue ( "sample_message_queue" , dbConnection ) ;
102
+ sample_message_queue . MessageType = OracleAQMessageType . Raw ;
103
+ OracleAQMessage msgIn = new OracleAQMessage ( ) ;
104
+ msgIn . Payload = Encoding . UTF8 . GetBytes ( "Another test payload" ) ;
105
+ sample_message_queue . Enqueue ( msgIn ) ;
106
+ Console . WriteLine ( "A message has been put into the test queue" ) ;
107
+ txn . Commit ( ) ;
108
+
109
+ txn = dbConnection . BeginTransaction ( ) ;
110
+ OracleAQMessage msgOut = sample_message_queue . Dequeue ( ) ;
111
+ txn . Commit ( ) ;
112
+ Console . WriteLine ( Encoding . UTF8 . GetString ( msgOut . Payload as byte [ ] ) ) ;
113
+ }
114
+ private static void Main ( string [ ] args )
115
+ {
116
+ try
117
+ {
118
+ string dbUrl , dbUsername , dbPassword ;
119
+
120
+ Console . Write ( "Please, provide database connection string : " ) ;
121
+ dbUrl = Console . ReadLine ( ) ;
122
+
123
+ Console . Write ( "Username : " ) ;
124
+ dbUsername = Console . ReadLine ( ) ;
125
+
126
+ Console . Write ( "Password : " ) ;
127
+ dbPassword = Console . ReadLine ( ) ;
128
+
129
+
130
+ OracleConnection dbConnection = createDBConnection ( dbUsername ,
131
+ dbPassword ,
132
+ dbUrl ) ;
133
+
134
+ basicJDBCTest ( dbConnection ) ;
135
+
136
+ basicAQTest ( dbConnection , "Test Message 01" ) ;
137
+
138
+ nativeAQTest ( dbConnection ) ;
139
+
140
+ closeDBConnection ( dbConnection ) ;
141
+ }
142
+ catch ( Exception e )
143
+ {
144
+ Console . WriteLine ( e ) ;
145
+ }
146
+ }
147
+ }
0 commit comments