Creating A Custom Collection

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

Guest

Hi,

I have an idea but I need some help. Basically I want to create an object
that is used to communicate between the front end of my application and the
database. This object will be used by my data layer to execute stored
procedures on the database. It will receve such parameters such as:

string StoredProcedureName, string UserName, string TransactionType and one
more which would be a collection of some sort. I would send this object to
send it an object that holds the parameters for the stored procedure so it
can walk this object to create a SqlCommand object and execute the stored
procedure.

The problem is that how do I create a multi demencial array where the first
column has a type of string and is called ParameterName, the 2nd column has a
type of System.Data.SqlClient.SqlDbType and be called ParameterDataType, and
the 3rd should be of type varient or object since and be called
ParameterValue.

The point of all this is to allow the business layer of the app to send the
name of the proc and the parameters object to the data layer and the data
layer can walk the parameter object and convert it to an SqlCommnad and
execute a stored procedure.

I think creating a custom collection may help but I have never used a
collection and it seems that I can only have one dimension in a collection or
a name value pair.

Any ideas?

Thanks
KP
 
Hello Kapil,

I have an introduction to implementing CollectionBase to create a strongly typed collection available on my website:

http://www.mattberther.com/2004/09/000540.html

You'll have to get away from using multidimensional arrays. I see an object in what you describe.

public class Parameter
{
public string Name;
public SqlDbType DataType;
public object Value;
}

I'm using fields as an example, you would probably go through and implement these as properties. From here, you'd create your CollectionBase derived implementation to work with this Parameter class.

You're resulting code would look something like this:

ParameterCollection coll = new ParameterCollection();
coll.Add(new Parameter("name", SqlDbType.Int, "value"));

Hope this helps...
 
Awesome Matt. Thats exactily what I needed. I think I got everything.

One more question. Now I created a method in my data layer called create
SQlCommand that will receve the Parameters Collection as a parameter, walk
the collection and create an SQLCommand object. I am using a

for(int Increment = 0; Increment < Collection.Items.Count; Increment++)

i would like to use a foreach() statement but my collection does not seem to
have that ability.

Any ideas?

Thanks very much.

Instead of using XML which is what we are doing now, I was able to increase
performance by 67 % across that application.
 
Back
Top