M
Massimo
I'm trying to write some DB-independent code using ADO.NET 2.0 and the
System.Data.Common.DbX classes, as suggested before; but I'm encountering
another weird problem.
Here's some code (let's assume the provider name and the connection string
are correct):
public void TestQuery(string providername, string connectionstring)
{
// Factory
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
// Connection
DbConnection dbconn = factory.CreateConnection();
dbconn.ConnectionString = connectionstring;
// Command Preparation
DbCommand command = dbconn.CreateCommand();
command.CommandText = "SELECT * FROM Table WHERE Column = @Val";
DbParameter param = factory.CreateParameter();
param.DbType = DbType.Byte;
param.PArameterName = "Val";
command.Parameters.Add(param);
// Execution
dbconn.Open();
command.Parameters["Val"].Value = 42;
command.ExecuteReader();
return;
}
As you can see, I'm trying to keep the code as provider-indipendent as
possibile; this is much semplified code, in the real code the provider
selection happens at program startup and is handled by some classes, the
comand preparation happens somewhat later and is handled by totally
different parts of the application, and the command execution happens at
runtime, almost everywhere; I want the last two phases to not know anything
about the specific provider used.
The problem: this code works only with SQL Server, which uses "@Val" as a
parameter placeholder. Oracle uses ":Val", and I'm told other providers use
even different characters ("?").
How can this be solved without cluttering the command building with
provider-based IFs and CASEs?
Thanks
Massimo
P.S.
I know I could write some code like
public string GeneratePlaceHolder(string paramname);
and use it when building commands, but I'd like something simpler (and
hopefully less ugly) to use.
System.Data.Common.DbX classes, as suggested before; but I'm encountering
another weird problem.
Here's some code (let's assume the provider name and the connection string
are correct):
public void TestQuery(string providername, string connectionstring)
{
// Factory
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
// Connection
DbConnection dbconn = factory.CreateConnection();
dbconn.ConnectionString = connectionstring;
// Command Preparation
DbCommand command = dbconn.CreateCommand();
command.CommandText = "SELECT * FROM Table WHERE Column = @Val";
DbParameter param = factory.CreateParameter();
param.DbType = DbType.Byte;
param.PArameterName = "Val";
command.Parameters.Add(param);
// Execution
dbconn.Open();
command.Parameters["Val"].Value = 42;
command.ExecuteReader();
return;
}
As you can see, I'm trying to keep the code as provider-indipendent as
possibile; this is much semplified code, in the real code the provider
selection happens at program startup and is handled by some classes, the
comand preparation happens somewhat later and is handled by totally
different parts of the application, and the command execution happens at
runtime, almost everywhere; I want the last two phases to not know anything
about the specific provider used.
The problem: this code works only with SQL Server, which uses "@Val" as a
parameter placeholder. Oracle uses ":Val", and I'm told other providers use
even different characters ("?").
How can this be solved without cluttering the command building with
provider-based IFs and CASEs?
Thanks
Massimo
P.S.
I know I could write some code like
public string GeneratePlaceHolder(string paramname);
and use it when building commands, but I'd like something simpler (and
hopefully less ugly) to use.