Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic methods for database driver access in ADO.NET

I have to write a small C# program which will handle at least three differents database vendors (Oracle, Sybase ASE, SqlServer) in a dynamic way. (It will rely on customer choices to choose the database)

I decided to use "pure" managed drivers through ado.net data providers.

But, when I just try connecting, I expected code a la "One line to rule them all", just like JDBC does with :

DriverManager.getConnection(connection_string);

Instead of this, surprised, I have to write for each driver its specific code :

SqlConnection() for SqlServer 

AseConnection() for Sybase

OracleConnection(), etc.

Of course, I should encapsulate -by myself- all of this inside abstract methods and dynamic loadings, but I'm wondering why such a thing doesn't already exist in .net

Mmhhh, I've got the feeling that I'm missing something

like image 297
Stef Avatar asked Oct 14 '13 10:10

Stef


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

Since you have the .Net Provider for he respective database installed on the machine, you can use the DbProviderFactory, for sample:

include

using System.Data.Common;

and try something like this:

// create the provider factory from the namespace provider
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); 

// you could create any other provider factory.. for Oracle, MySql, etc...


// use the factory object to create Data access objects.
DbConnection connection = factory.CreateConnection(); // will return the connection object, in this case, SqlConnection ...

connection.ConnectionString = "read connection string from somewhere.. .config file for sample";


try
{
   // open connection
   connection.Open();

   // create command to execute queries...
   DbCommand command = connection.CreateCommand(); // create a SqlCommand, OracleCommand etc... depende of the provider factory configured.

   // some logic 
}
catch 
{
}
finally 
{
   // close connection
   connection.Close();
}

To know, what providers your application can find, you can use the DbProviderFactories.GetFactoryClasses() method to get a DataTable with details of every provider installed on the machine.

like image 185
Felipe Oriani Avatar answered Sep 20 '22 15:09

Felipe Oriani