Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter Delimiters

I have C# application we've been coding in ADO.NET. I've been using the IDbCommand, and IDbConnection interfaces to create cross-database code.

It's all worked wonderfully so far (across Firebird, SQLite, SQL Server 2005, and 2008, Access 2007, and Access 2010, and Oracle 11g).

Problem I have, is I now have an Oracle 10g database I need to support.

All the "normal" stuff, creating connections, and commands works fine, however, when I go to create a parameter using the interface IDataParameter and cmd.CreateParamater() fails on 10g, because of parameter syntax in the query (I'm using parameterized queries).

Apparently, Oracle 10g, out of the box, doesn't support using the @ sign. Oracle 11g, SQL Server, and all the others mentioned do.

For instance, the following query will fail in 10g:

select * from Products where ProductId = @ProductId

But, if I use the colon, it succeeds just fine, using the above mentioned ado.net interfaces, so this query will succeed:

select * from Products where ProductId = :ProductId

Unfortunately, the colon doesn't work in most of the other database implementations.

Or is there an option that can be flipped in the Oracle 10g Database that allows for @ delimiter to be used in place of the : delimiter for parameters.

The current solution I have is less than ideal, I have the customer/client initializing the property ParameterDelimiter (that I default to the @ sign), and use a string.Format, to insert the ParameterDelimiter.

Is there any standard way of doing this that I'm missing, without having the customer have to pass me a delimiter, or without having my base libraries know about the database implementation? (For instance, including ODP.NET and checking against an OracleConnection)

like image 455
Brian Deragon Avatar asked Feb 23 '23 02:02

Brian Deragon


1 Answers

For what it's worth, I did find this post:

Which parameter marker should I use? ADO.NET 2.0, Sql Server @, Oracle : (link is dead)

mentioned in this question:

Get the parameter prefix in ADO.NET

With the following code 'asking' the connection object for the information:

string format = connection
  .GetSchema("DataSourceInformation")
  .Rows[0]["ParameterMarkerFormat"]
  .ToString();

So that should be the 'standard way of doing this', also without having the customer pass the information and without having to know about the database implementation.

Edit: it must be added that System.Data.SqlClient.SqlConnection apparently returns {0} instead of @{0}.

like image 137
Michiel van Oosterhout Avatar answered Feb 24 '23 15:02

Michiel van Oosterhout