H
hardieca
Good morning,
I am creating a CMS which I would like to support multiple DBs for its
back-end. I have taken the approach of using DBProviderFactories to
keep my code flexible enough so that I can switch backends simply by
changing the provider and connection strings stored in my App.config
file.
However, there are a few methods I would like to add to the Command
object to wrap up a few inconsistencies of how DBs implement certain
functionality. For instance, I would like the command object to return
the ID of the last inserted record.
I would like to add something like the following to the Command object:
public int GetLastInserted(){
string oldSQL = this.CommandText;
if (provider = "System.Data.SqlClient){
this.CommandText = @"SELECT Cast(scope_identity() AS int;";
}
else if (provider = "MySQL.Data.MySqlClient"){
this.CommandText = @"SELECT currval();";
}
this.CommandText = oldSQL;
return (int)this.ExecuteScalar();
So when I create my Command object like so:
DbProviderFactory factory = DBProviderFactories.GetFactory(provider);
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = conStr;
DbCommand cmd = conn.CreateCommand();
Now that I have the Command object cmd, can I somehow cast/convert it
into a custom type that includes the desired members?
Kind regards,
Chris
I am creating a CMS which I would like to support multiple DBs for its
back-end. I have taken the approach of using DBProviderFactories to
keep my code flexible enough so that I can switch backends simply by
changing the provider and connection strings stored in my App.config
file.
However, there are a few methods I would like to add to the Command
object to wrap up a few inconsistencies of how DBs implement certain
functionality. For instance, I would like the command object to return
the ID of the last inserted record.
I would like to add something like the following to the Command object:
public int GetLastInserted(){
string oldSQL = this.CommandText;
if (provider = "System.Data.SqlClient){
this.CommandText = @"SELECT Cast(scope_identity() AS int;";
}
else if (provider = "MySQL.Data.MySqlClient"){
this.CommandText = @"SELECT currval();";
}
this.CommandText = oldSQL;
return (int)this.ExecuteScalar();
So when I create my Command object like so:
DbProviderFactory factory = DBProviderFactories.GetFactory(provider);
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = conStr;
DbCommand cmd = conn.CreateCommand();
Now that I have the Command object cmd, can I somehow cast/convert it
into a custom type that includes the desired members?
Kind regards,
Chris