Passing type Object to SqlParameter.Value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am creating my own data access layer (I know I am re-inventing the wheel
but I like the challenge).

At the moment I am able to pass in a HashTable with the key and parameter
value and I use the key to get the SqlDbType from a config file. From there
I set up the parameters required for the backend Sp's.

The trouble is, I am left with something like this:

foreach(DictionaryEntry de in hashTable)
{
string key = de.Key;
object val = de.Value; //I don't yet know the type

//Create the parameter (called param) and assign the name and type here

Param.Value = val;

//Other stuff here
}

Is it okay to pass an object as the value, or should I cast it to the
correct type first? Is there any significant overhead here?

I thought about getting the type from the SqlParameter once I had set it up
but that would give me an SqlDbType rather than a System.Type.

Can anyone suggest a way to cast this (other than retrieve them from another
config file), or is it okay to pass the value as an object?

Any help is always appreciated.
 
Hi,

What is the goal that you do this, I as I see this, then I often get the
idea that it is more the chalenge to build something like this then that it
is functional.

(Remember others had easily to understand it to make it maintainable).

Therefore what is it that you want to achieve?

Cor
 
Cor,

the idea is that I can populate a HashTable with the name of the parameter
and it's value and pass it to the data layer, where these values are used to
construct the SqpParameters needed for the SqlCommand.

I thought about creating the SqlParameters in the UI and passing an array of
them to the data access layer, but then I am splitting the work of the data
layer between two layers.

My idea is that whoever is using the data layer can simply pass in the name
of the parameter and it's value and not worry about anything else, making
it's use extremely simple.

Just to let you know how I am doing it, I have a constants file who's
members map to the fields in the database. The user selects the constant
they need and the value for the parameter and add it to the HashTable as the
key/value pair. They pass these to a method in the data layer and it does
the rest.
 
Hi,

You know that adding a parameter to the parametercollection is extremely
simple.


WhaterCommand.Parameters.Add(new SqlParameter("@TheFirstOne", "TheValue")
with an ending ";" for C#.

Mostly you see extreme difficult samples.

I would not make it more difficult than it is.

Cor
 
Cor,

all this time I have been using SqlParameters and wasn't aware of that
overload.

I shall implement that now.

All I need to do now is find a way to tell the data layer to use a specific
config file (rather than the web.config) and I am well on my way.

Many thanks,

Mac
 
Back
Top