473,666 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlParameter Design Problem

Hi,

I am currently writing a method for calling stored procedures. I want the
method to be able to handle variable amount of parameters and need some
advice on the design of this.

The way I see it, there is two ways of doing this.

Number 1 being a method for each stored procedure.
Each of these procedures would call the appropiate SP and also build up the
SqlParameters in there.
This could be a bit time consuming and difficult to maintain. My class would
be messy with various different methods just for calling SP.

The second way is to create a generic method which calls a SP by a parameter
name passed and also an array of SqlParameters. This means the client is
responsible for setting up an parameters required for the SP (name, data
type).
The SP method simply loops round the array adding all the SqlParameters.

Thats my thoughts on the matter.

Does anyone have any other ways of doing this, or opinions on what I have
just described ?

--
Steven
www.stevenblair.com
Nov 15 '05 #1
2 1309
Hi Steven

This is how I would do it

My SP would contain the logic to hand diferent prams, e.g.

ALTER PROCEDURE InsertCupDetail
@CupId INT,
@Msg VARCHAR(8000),
@Time DateTime =null
AS

if @Time is null
INSERT INTO Detail (Cup, Msg) VALUES (@CupId, @Msg)
else
INSERT INTO Detail (Cup, Msg, Time) VALUES (@CupId, @Msg,@Time)

Then if I am not passing a pram, I just pass null to the prameter.

You may want to create overloaded methods to call the sp, e.g.

private void InsertCupDetail (int CupId)
{
InsertCupDetail (int CupId,'');
}
private void InsertCupDetail (int CupId, string Msg)
{
InsertCupDetail (int CupId,'',null);
}
private void InsertCupDetail (int CupId, string Msg, DateTime Time)
{
call procedure....
}

Gary
"Steven Blair" <st**********@b tinternet.com> wrote in message
news:bo******** **@hercules.bti nternet.com...
Hi,

I am currently writing a method for calling stored procedures. I want the
method to be able to handle variable amount of parameters and need some
advice on the design of this.

The way I see it, there is two ways of doing this.

Number 1 being a method for each stored procedure.
Each of these procedures would call the appropiate SP and also build up the SqlParameters in there.
This could be a bit time consuming and difficult to maintain. My class would be messy with various different methods just for calling SP.

The second way is to create a generic method which calls a SP by a parameter name passed and also an array of SqlParameters. This means the client is
responsible for setting up an parameters required for the SP (name, data
type).
The SP method simply loops round the array adding all the SqlParameters.

Thats my thoughts on the matter.

Does anyone have any other ways of doing this, or opinions on what I have
just described ?

--
Steven
www.stevenblair.com

Nov 15 '05 #2
> "Steven Blair" <st**********@b tinternet.com> wrote in message
news:bo******** **@hercules.bti nternet.com...
Hi,

I am currently writing a method for calling stored procedures. I want the
method to be able to handle variable amount of parameters and need some
advice on the design of this.


Steven,

This works well for multiple stored procedures with a variable number of parameters:

objCommand = New SQLCommand(spNa me,objConnectio n)

With objCommand
.CommandType = CommandType.Sto redProcedure
.Parameters.Cle ar()
End With

With objCommand
.Parameters.Add ("@prmcName" , name)
.Parameters.Add ("@prmcAddress" , address)
.Parameters.Add ("@prmcCSZ", citystatezip)
.Parameters.Add ("@prmcDM", pdm)
End With

objConnection.O pen()

objDataAdapter. Fill(mobjDataSe t)

objConnection.C lose()

Or, if using an insert or update command:

objConnection.O pen()
objCommand.Exec uteNonQuery()
objConnection.C lose()
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3117
by: Kenneth | last post by:
Can anyone explain me why it is neccesary to include SqlDbType to the SqlParameter. In every example I see, it is done, but no one explaines why. I have for example a date I want to save into my Sql Server database through a stored procedure-call. In the database it is defined as a SmallDateTime. Every 3 methods in the client-code below gives the same (and correct) result. So why is it that important? dim param As SqlParameter
3
2443
by: jiangyh | last post by:
hi everyone: SqlParameter class has a property that is DbType looks like following source public DbType DbType { get { // TODO: Add DBParameter.DbType getter implementation return new DbType ();
5
8291
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is not working. The Address column is DataType NVarChar 90, no problem. Any idea?
2
301
by: Stephan | last post by:
Hi, I'm experiencing the problem mentioned in the title above. Briefly, I build a page in which I include a class. This class contains a procedure to add a value to an ArrayList. The value is hold by a structure. So all elements in the arraylist are in fact a structure. As you can see in the code below. Now what I'm trying to do is for each value in the Arraylist create and add a parameter. Then I try to execute a SP. This only works if...
0
2750
by: Elliot M. Rodriguez | last post by:
I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of input parameters, I created a function signature that accepts a ParamArray of SqlParameters as well as the name of the stored proc. In the body of the function I loop through the param array and append each object to the Parameters collection of...
3
2757
by: Stacey Levine | last post by:
I have a webservice that has the below procedure. Basically a procedure to called a stored procedure and return the results. When I try to call the webservice from my program I get the error. Both my calling code and the webservice code are below. Thanks for your help. D:\Projects .NET\StoreBO\frmVoids.vb(182): Value of type '1-dimensional array of System.Data.SqlClient.SqlParameter' cannot be converted to '1-dimensional array of...
0
1693
by: Chris Crowe [MVP 1997 -> 2006] | last post by:
I am having major problems using GetStoredProcCommand and using Parameters in the same function call in the latest builds of the Enterprise Library Data. The error I get is : Failed to convert parameter value from a SqlParameter to a String. Here is some code that works and does not work.
6
7834
by: Tim Zych | last post by:
' Declare a new parameter object Dim param() As SqlParameter = New SqlParameter(0) {} ' Set this to null and make it an InputOutput parameter param(0) = New SqlParameter("@Something, DBNull.Value) ' Can also be non-null, but sometimes is null param(0).Direction = ParameterDirection.InputOutput ' But, before we begin, store a copy of the existing parameters into another variable Dim param2() As SqlParameter = New SqlParameter(0) {}...
1
2119
by: tsorgi | last post by:
I'm writing a data conversion application to support a system migration. The code is reading formatted text files and loading them into MS SQL 2005 database tables. I wanted to call the existing INSERT stored procedures in the database to perform the update, yet I don't want to hard-code all the parameter names. Below is a bit of code that reads the imported data from the 'Data' DataTable, and prepares to insert it into the database table...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8785
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.