Skip to content

Commit 7a6b941

Browse files
authored
Create async.cs (#319)
1 parent 07cfb27 commit 7a6b941

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

samples/async/async.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Oracle.ManagedDataAccess.Client;
2+
using System.Threading.Tasks;
3+
using System.Threading;
4+
using System;
5+
6+
// This code sample demonstrates using asynchronous ODP.NET (managed or core) and times its execution time.
7+
// This sample uses the Oracle HR sample schema.
8+
9+
class ODPNET_Async
10+
{
11+
public static async Task Main()
12+
{
13+
// Add password and data source to connect to your Oracle database
14+
string conString = @"User Id=hr;Password=<PASSWORD>;Data Source=<NET SERVICE NAME>;";
15+
16+
using (OracleConnection con = new OracleConnection(conString))
17+
{
18+
// Measure time OpenAsync takes before next operation can start execution
19+
DateTime start_time = DateTime.Now;
20+
Task task = con.OpenAsync();
21+
DateTime end_time_open = DateTime.Now;
22+
23+
// Simulate operation that takes one second
24+
Thread.Sleep(1000);
25+
26+
string cmdText = "SELECT * FROM EMPLOYEES FETCH FIRST 100 ROWS ONLY";
27+
using (OracleCommand cmd = new OracleCommand(cmdText, con))
28+
{
29+
// Retrieve open connection with "await"
30+
await task;
31+
32+
// Execute SELECT statement asynchronously
33+
using (OracleDataReader reader = await cmd.ExecuteReaderAsync())
34+
{
35+
// Retrieve results asynchronously
36+
await reader.ReadAsync();
37+
}
38+
}
39+
// Measure time all the async operations took
40+
DateTime end_time_all = DateTime.Now;
41+
42+
// Calculate connection open time and write result to console
43+
TimeSpan ts_open = end_time_open - start_time;
44+
double ts_open1 = Math.Round(ts_open.TotalSeconds, 2);
45+
Console.WriteLine("Asynchronous connection open time: " + ts_open1 + " seconds");
46+
47+
// Calculate overall operation time and write to console
48+
TimeSpan ts_all = end_time_all - start_time;
49+
double ts_all1 = Math.Round(ts_all.TotalSeconds, 2);
50+
Console.WriteLine("Asynchronous ODP.NET operations time: " + ts_all1 + " seconds");
51+
}
52+
}
53+
}
54+
55+
/* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. */
56+
57+
/******************************************************************************
58+
* Licensed under the Apache License, Version 2.0 (the "License");
59+
* you may not use this file except in compliance with the License.
60+
* You may obtain a copy of the License at
61+
*
62+
* http://www.apache.org/licenses/LICENSE-2.0
63+
*
64+
* Unless required by applicable law or agreed to in writing, software
65+
* distributed under the License is distributed on an "AS IS" BASIS,
66+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
67+
* See the License for the specific language governing permissions and
68+
* limitations under the License.
69+
*
70+
*****************************************************************************/

0 commit comments

Comments
 (0)