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