views:

572

answers:

2

I am writing a small framework in C# for importing data to SQL Server.

I have noticed that if you try to use the ? placeholder for parameters, they do not work if you use the System.Data.SqlClient namespace.

However, if you use the System.Data.OleDb namespace for working with the database, they work just fine. (you just have to add Provider=SQLOLEDB in your connect string)

So I have a few questions, because the ? syntax is required by my solution:

  1. Is there an alternative to using named parameters with System.Data.SqlClient? I want to use ordered parameters.
  2. Is there any performance impact to using one namespace (ado provider) over the other?
  3. Is there any other reason I should prefer one namespace over the other?

To elaborate on what I'm trying to do, my framework is going to take a class decorated with some custom attributes and generate SQL like so:

INSERT INTO myTable (col1, col2, ...) VALUES (?, ?, ...)

This string will be created with one IDbCommand and several IDataParameters and for each ExecuteNonQuery it will merely set the values on the IDataParameters.

Any thoughts?

Edit: I tried using named parameters as p0, p1, etc. as an answerer mentioned and it is working pretty well on SqlClient. However if I use Oledb, it errors saying:

"Must declare the scalar variable @p0."

My parameter is created like so:

new OleDbParameter("p" + index, GetType(attribute)); 
//GetType does work to get the correct SqlDbType

What is wrong? I also tried adding and removing the @ symbol in the parameter name to no avail.

FINAL EDIT: I just made Oledb use ? and SqlClient use named parameters. I can change back and forth for new providers.

A: 

You can use this as a ref for the params

SqlCommand.Parameters Property

astander
I'd still rather look them up by index because it is faster to pull out a parameter by index than it's name by a string value.
Jonathan.Peppers
@Jonathan: how much faster, at what expense of maintainability, and have you benchmarked?
gbn
My framework is meant for speed and I would gladly sacrifice maintainability for speed in this case. Once completed, no changes should really be needed other than to add support for other database providers.
Jonathan.Peppers
+2  A: 

Thats the behaviour of those classes. OleDb uses ?, SqlClient named parameter.

Perfomance: I have no benchmarks, but I think the SqlClient should perform better with an Sql Server than OleDB. SqlClient is explicit for Sql Server, OleDB for any kind of Datasource (even Excel). But pls. try it out.

With SqlClient you can also have "ordered parameter". Just call you parameter @Param1, @Param2, @Param3....

Arthur
Using named parameters is slightly more complicated for some things I'm doing. I eventually want my framework to work for other providers such as Oledb(Excel or Access), MySQL, etc. Does OleDb have named parameters at all?
Jonathan.Peppers
I agree with all of the above (and when using OLEDB I use named params anyway for clarity even though its only the position that counts).
Murph
@Jonathan - yes, as I said, you can use named paramters in OLE DB, its just that the only thing that actually matters is the position
Murph
@Jonathan: If you plan to use other provider than using OleDb would be the simple way. Another way would be to abstract your DataAccesLayer. This is would be necessary if the SQL Syntax is different. But that's another story.
Arthur
See my edit above.
Jonathan.Peppers