M
Mr. X.
Hello,
How can I expose to C# the list of all DB-Drivers, and are all the drivers
must be on ODBC ?
Thanks
How can I expose to C# the list of all DB-Drivers, and are all the drivers
must be on ODBC ?
Thanks
How can I expose to C# the list of all DB-Drivers, and are all the drivers
must be on ODBC ?
How can I expose to C# the list of all DB-Drivers, and are all the
drivers must be on ODBC ?
Mr. X. said:Also.
If I use MySql :
How can I use the SqlConnection, SqlDataAdapter, etc. instead of
MySqlConnection, MySqlDataAdapter.
(When creating and object - I think I should use MySqlConnection, etc
... but when refer to the parent object type : SqlConnection etc ... Is
that right in OOP ?)
Mr. X. said:Well I have found something :
http://www.dotnetjohn.com/articles.aspx?articleid=244
It is not compiled !
Take a look at this code, and let me know if that solve situation when I
need to use several kind of databases.
If there any some better example, I would be glade to know about.
I need to understand this code :
(I have added the following to DBManagerFactory)
public static IDbCommand GetCommand(DataProvider providerType, string
qry, IDBManager aConnMgr)
{
switch (providerType)
{
case DataProvider.SqlServer:
return new SqlCommand(qry, aConnMgr.Connection);
case DataProvider.OleDb:
return new OleDbCommand(qry, aConnMgr.Connection);
case DataProvider.Odbc:
return new OdbcCommand(qry, aConnMgr.Connection);
case DataProvider.Oracle:
return new OracleCommand(qry, aConnMgr.Connection);
case DataProvider.MySql:
return new MySqlCommand(qry, aConnMgr.Connection);
default:
return null;
}
}
Every new is not compiled (I.e oledb) :
The best overloaded method match for
'System.Data.OleDb.OleDbCommand.OleDbCommand(string,
System.Data.OleDb.OleDbConnection)' has some invalid arguments
Argument '2': cannot convert from 'System.Data.IDbConnection' to
'System.Data.OleDb.OleDbConnection'
Need to understand what's wrong, and what should I write instead.
Arne Vajhøj said:I need to understand this code :
(I have added the following to DBManagerFactory)
public static IDbCommand GetCommand(DataProvider providerType, string
qry, IDBManager aConnMgr)
{
switch (providerType)
{
case DataProvider.SqlServer:
return new SqlCommand(qry, aConnMgr.Connection);
case DataProvider.OleDb:
return new OleDbCommand(qry, aConnMgr.Connection);
case DataProvider.Odbc:
return new OdbcCommand(qry, aConnMgr.Connection);
case DataProvider.Oracle:
return new OracleCommand(qry, aConnMgr.Connection);
case DataProvider.MySql:
return new MySqlCommand(qry, aConnMgr.Connection);
default:
return null;
}
}
Every new is not compiled (I.e oledb) :
The best overloaded method match for
'System.Data.OleDb.OleDbCommand.OleDbCommand(string,
System.Data.OleDb.OleDbConnection)' has some invalid arguments
Argument '2': cannot convert from 'System.Data.IDbConnection' to
'System.Data.OleDb.OleDbConnection'
Need to understand what's wrong, and what should I write instead.
That code looks very fishy.
If you need a IDbCommand and you have an IDbConnection,
then you do not make a switch on the type but simply call the
CreateCommand method.
Example:
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
class MultiDb2
{
private static void test(string provider, string constr)
{
DbProviderFactory dbf = DbProviderFactories.GetFactory(provider);
IDbConnection con = dbf.CreateConnection();
con.ConnectionString = constr;
con.Open();
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLine(f1 + " " + f2);
}
con.Close();
}
public static void Main(string[] args)
{
test("System.Data.OleDb", @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Databases\MSAccess\Test.mdb");
test("System.Data.SqlClient", "server=ARNEPC3;Integrated
Security=SSPI;database=Test");
}
}
Applied to your method then I think it can be done as:
public static IDbCommand GetCommand(DataProvider providerType, string qry,
IDBManager aConnMgr)
{
IDbCommand res = aConnMgr.Connection.CreateConnection;
res.CommandText = qry;
return res;
}
Much simpler !!
Arne
Arne Vajhøj said:I need to understand this code :
(I have added the following to DBManagerFactory)
public static IDbCommand GetCommand(DataProvider providerType, string
qry, IDBManager aConnMgr)
{
switch (providerType)
{
case DataProvider.SqlServer:
return new SqlCommand(qry, aConnMgr.Connection);
case DataProvider.OleDb:
return new OleDbCommand(qry, aConnMgr.Connection);
case DataProvider.Odbc:
return new OdbcCommand(qry, aConnMgr.Connection);
case DataProvider.Oracle:
return new OracleCommand(qry, aConnMgr.Connection);
case DataProvider.MySql:
return new MySqlCommand(qry, aConnMgr.Connection);
default:
return null;
}
}
Every new is not compiled (I.e oledb) :
The best overloaded method match for
'System.Data.OleDb.OleDbCommand.OleDbCommand(string,
System.Data.OleDb.OleDbConnection)' has some invalid arguments
Argument '2': cannot convert from 'System.Data.IDbConnection' to
'System.Data.OleDb.OleDbConnection'
Need to understand what's wrong, and what should I write instead.
That code looks very fishy.
If you need a IDbCommand and you have an IDbConnection,
then you do not make a switch on the type but simply call the
CreateCommand method.
Example:
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
class MultiDb2
{
private static void test(string provider, string constr)
{
DbProviderFactory dbf = DbProviderFactories.GetFactory(provider);
IDbConnection con = dbf.CreateConnection();
con.ConnectionString = constr;
con.Open();
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLine(f1 + " " + f2);
}
con.Close();
}
public static void Main(string[] args)
{
test("System.Data.OleDb", @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Databases\MSAccess\Test.mdb");
test("System.Data.SqlClient", "server=ARNEPC3;Integrated
Security=SSPI;database=Test");
}
}
Applied to your method then I think it can be done as:
public static IDbCommand GetCommand(DataProvider providerType, string qry,
IDBManager aConnMgr)
{
IDbCommand res = aConnMgr.Connection.CreateConnection;
res.CommandText = qry;
return res;
}
Much simpler !!
Arne
I see that the example on the link
http://www.dotnetjohn.com/articles.aspx?articleid=244 is too complicated.
The DbProviderFactory does all the thing.
How can I create DataAdapter (something simmiliar to CreateCommand) ?
For MySql : Isn't any MySql for ODBC (Ole DB, ADO.NET) ?
If not - is there any common parent object I can use for each driver?
Yes, that's the sort of thing I was suggesting.
Now you can have fun adding code for the MySQL provider - assuming it
implements the same interfaces as all the others.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.