filibuster
Programmer
Hi all,
I am playing around with database access from C#.
I got some working code for connecting to oracle (complete snippets included below) using two different strategies:
- ODBC access via OdbcConnection
- OLE access via OleDbConnection
The problem I am facing is that I do have to write the same code twice, because OleDbConnection and OdbcConnection do not share the same super class (just look at the two samples, both pieces of code are pretty much the same).
In my opinion this is a major drawback compared to JDBC where I only have to change the driver specification and that's it. (this is because database access in JDBC is defined through plain APIs).
This becomes an issue when changing the target db or the database configuration.
The class hierarchy is as follows:
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Microsoft.Data.Odbc.OdbcConnection
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.OleDb.OleDbConnection
Am I missing something ??
/** OracleOdbcDemo.cs */
using System;
using System.Data;
//using System.Data.Odbc;
using Microsoft.Data.Odbc;
/** demo for accessing oracle database within C# through the Odbc class.
This is similar to using the JDBC-ODBC bridge in Java/JDBC
To make the whole thing work, a datasource named "oracle_odbc" must
be available on your machine.
To run this sample, you have to install separately ODBC.NET from the following site:
A new directory named "Odbc.Net" will be created in your Microsoft.NET folder, e.g:
C:\Program Files\Microsoft.NET\Odbc.Net
The directory contains some txt and help files, and most importantly, a file called "Microsoft.Data.Odbc.dll"
You will need this file to compile your C# program as follows:
csc /r:Microsoft.Data.Odbc.dll OracleOdbcDemo.cs
*/
class OracleOdbcDemo {
public static void Main() {
try {
Console.WriteLine("Oracle Connectivity through ODBC DB with C#"
// Create/Open Oracle Connection
String dataSourceName = "oracle_odbc";
String user = "system";
String password = "manager";
// e.g. "Provider=MSDAORA;DSN=zwu.world;UID=system;PWD=manager
String connectionString =
"Provider=MSDAORA" +
";DSN=" + dataSourceName +
";UID=" + user +
";PWD=" + password;
Console.WriteLine("Oracle Connection:"
Console.WriteLine(" dataSourceName=[" + dataSourceName + "]"
Console.WriteLine(" user=[" + password + "]"
Console.WriteLine(" password=[" + password + "]"
Console.WriteLine(" connectionString=[" + connectionString + "]"
Console.WriteLine("Creating connection"
OdbcConnection connection = new OdbcConnection(connectionString);
Console.WriteLine("Opening connection"
connection.Open();
// Create Oracle Command
Console.WriteLine("Creating command"
OdbcCommand command = connection.CreateCommand();
command.CommandText = "select table_name from user_tables";
Console.WriteLine("CommandText=[" + command.CommandText + "]"
// Execute Oracle Command
Console.WriteLine("Executing Command/Reader"
Console.WriteLine(""
OdbcDataReader reader = command.ExecuteReader();
// Display Results
Console.WriteLine("table_name"
while (reader.Read()) {
Console.WriteLine(" " + reader.GetString(0));
}
Console.WriteLine(""
// Cleanup
Console.WriteLine("Closing Reader"
reader.Close();
Console.WriteLine("Closing Connection"
connection.Close();
Console.WriteLine("Complete"
} catch(Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
/** OracleOleDbDemo.cs */
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
/** demo for accessing oracle database within C# through the OleDb class.
This is similar to using Oracle's "Thick" OCI Driver in Java/JDBC.
To make the whole think work, Oracle TNS must be set up properly on your
client machine (this is the program where the .exe file will be run).
To verify your TNS Setup, type in the following
tnsping oracle
(or whatever value the tnsName variable is given below)
*/
class OracleOleDbDemo {
public static void Main() {
try {
Console.WriteLine("Oracle Connectivity through OLE DB with C#"
// Create/Open Oracle Connection
String tnsName = "oracle";
String user = "system";
String password = "manager";
String connectionString =
"Provider=OraOLEDB.Oracle" +
";Data Source=" + tnsName +
";User ID=" + user +
";Password=" + password;
Console.WriteLine("Oracle Connection:"
Console.WriteLine(" tnsName=[" + tnsName + "]"
Console.WriteLine(" user=[" + password + "]"
Console.WriteLine(" password=[" + password + "]"
Console.WriteLine(" connectionString=[" + connectionString + "]"
Console.WriteLine("Creating connection"
OleDbConnection connection = new OleDbConnection(connectionString);
Console.WriteLine("Opening connection"
connection.Open();
// Create Oracle Command
Console.WriteLine("Creating command"
OleDbCommand command = connection.CreateCommand();
command.CommandText = "select table_name from user_tables";
Console.WriteLine("CommandText=[" + command.CommandText + "]"
// Execute Oracle Command
Console.WriteLine("Executing Command/Reader"
Console.WriteLine(""
OleDbDataReader reader = command.ExecuteReader();
// Display Results
Console.WriteLine("table_name"
while (reader.Read()) {
Console.WriteLine(" " + reader.GetString(0));
}
Console.WriteLine(""
// Cleanup
Console.WriteLine("Closing Reader"
reader.Close();
Console.WriteLine("Closing Connection"
connection.Close();
Console.WriteLine("Complete"
} catch(Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
I am playing around with database access from C#.
I got some working code for connecting to oracle (complete snippets included below) using two different strategies:
- ODBC access via OdbcConnection
- OLE access via OleDbConnection
The problem I am facing is that I do have to write the same code twice, because OleDbConnection and OdbcConnection do not share the same super class (just look at the two samples, both pieces of code are pretty much the same).
In my opinion this is a major drawback compared to JDBC where I only have to change the driver specification and that's it. (this is because database access in JDBC is defined through plain APIs).
This becomes an issue when changing the target db or the database configuration.
The class hierarchy is as follows:
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Microsoft.Data.Odbc.OdbcConnection
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.OleDb.OleDbConnection
Am I missing something ??
/** OracleOdbcDemo.cs */
using System;
using System.Data;
//using System.Data.Odbc;
using Microsoft.Data.Odbc;
/** demo for accessing oracle database within C# through the Odbc class.
This is similar to using the JDBC-ODBC bridge in Java/JDBC
To make the whole thing work, a datasource named "oracle_odbc" must
be available on your machine.
To run this sample, you have to install separately ODBC.NET from the following site:
A new directory named "Odbc.Net" will be created in your Microsoft.NET folder, e.g:
C:\Program Files\Microsoft.NET\Odbc.Net
The directory contains some txt and help files, and most importantly, a file called "Microsoft.Data.Odbc.dll"
You will need this file to compile your C# program as follows:
csc /r:Microsoft.Data.Odbc.dll OracleOdbcDemo.cs
*/
class OracleOdbcDemo {
public static void Main() {
try {
Console.WriteLine("Oracle Connectivity through ODBC DB with C#"
// Create/Open Oracle Connection
String dataSourceName = "oracle_odbc";
String user = "system";
String password = "manager";
// e.g. "Provider=MSDAORA;DSN=zwu.world;UID=system;PWD=manager
String connectionString =
"Provider=MSDAORA" +
";DSN=" + dataSourceName +
";UID=" + user +
";PWD=" + password;
Console.WriteLine("Oracle Connection:"
Console.WriteLine(" dataSourceName=[" + dataSourceName + "]"
Console.WriteLine(" user=[" + password + "]"
Console.WriteLine(" password=[" + password + "]"
Console.WriteLine(" connectionString=[" + connectionString + "]"
Console.WriteLine("Creating connection"
OdbcConnection connection = new OdbcConnection(connectionString);
Console.WriteLine("Opening connection"
connection.Open();
// Create Oracle Command
Console.WriteLine("Creating command"
OdbcCommand command = connection.CreateCommand();
command.CommandText = "select table_name from user_tables";
Console.WriteLine("CommandText=[" + command.CommandText + "]"
// Execute Oracle Command
Console.WriteLine("Executing Command/Reader"
Console.WriteLine(""
OdbcDataReader reader = command.ExecuteReader();
// Display Results
Console.WriteLine("table_name"
while (reader.Read()) {
Console.WriteLine(" " + reader.GetString(0));
}
Console.WriteLine(""
// Cleanup
Console.WriteLine("Closing Reader"
reader.Close();
Console.WriteLine("Closing Connection"
connection.Close();
Console.WriteLine("Complete"
} catch(Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
/** OracleOleDbDemo.cs */
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
/** demo for accessing oracle database within C# through the OleDb class.
This is similar to using Oracle's "Thick" OCI Driver in Java/JDBC.
To make the whole think work, Oracle TNS must be set up properly on your
client machine (this is the program where the .exe file will be run).
To verify your TNS Setup, type in the following
tnsping oracle
(or whatever value the tnsName variable is given below)
*/
class OracleOleDbDemo {
public static void Main() {
try {
Console.WriteLine("Oracle Connectivity through OLE DB with C#"
// Create/Open Oracle Connection
String tnsName = "oracle";
String user = "system";
String password = "manager";
String connectionString =
"Provider=OraOLEDB.Oracle" +
";Data Source=" + tnsName +
";User ID=" + user +
";Password=" + password;
Console.WriteLine("Oracle Connection:"
Console.WriteLine(" tnsName=[" + tnsName + "]"
Console.WriteLine(" user=[" + password + "]"
Console.WriteLine(" password=[" + password + "]"
Console.WriteLine(" connectionString=[" + connectionString + "]"
Console.WriteLine("Creating connection"
OleDbConnection connection = new OleDbConnection(connectionString);
Console.WriteLine("Opening connection"
connection.Open();
// Create Oracle Command
Console.WriteLine("Creating command"
OleDbCommand command = connection.CreateCommand();
command.CommandText = "select table_name from user_tables";
Console.WriteLine("CommandText=[" + command.CommandText + "]"
// Execute Oracle Command
Console.WriteLine("Executing Command/Reader"
Console.WriteLine(""
OleDbDataReader reader = command.ExecuteReader();
// Display Results
Console.WriteLine("table_name"
while (reader.Read()) {
Console.WriteLine(" " + reader.GetString(0));
}
Console.WriteLine(""
// Cleanup
Console.WriteLine("Closing Reader"
reader.Close();
Console.WriteLine("Closing Connection"
connection.Close();
Console.WriteLine("Complete"
} catch(Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}