DAAB: Need Help writing GetStoredProcCommand(String, Object [])

Topics: Data Access Application Block
Jan 20, 2010 at 4:20 PM

I want to use this command to pass three string parameters: @Category, @City, and @State (in this order), which are supplied by the user of my program.

The variables will be entered using three comboBoxes where the user selects these three variables - category item, city item, and state item, which will then be declared and assigned in the following manner: (that's the plan anyway)

string category, city, state;

category = comboBox1.Text;

city = comboBox2.Text;

state = comboBox3.Text;

I'm not sure how to use my variables in the GetStoredProcCommand(String, Object[]), to pass the parameters to the method.

I'm using SQL Server 2008 Express, Visual Studio 2008 Express, C# .NET v3.5, and the program is a WinForm application.

Should my variable declaration be as written above, or should I write:

object [ ] o = new object[ ] {category, city, state}; 

o[0] = category;

o[1] = city;

o[2] = state;

...

and then ...what??

As you can see, I have gotten myself all turned around.

Any help will be appreciated

Jan 20, 2010 at 8:58 PM

I personally find it much cleaner to use the AddInParameter function.  For example:

 

Database db = DatabaseFactory.CreateDatabase();

DbCommand command = db.GetStoredProcCommand("nameOfYourSproc");
db.AddInParameter(command, "@category", DbType.String, category);
db.AddInParameter(command, "@city", DbType.String, city);
db.AddInParameter(command, "@state", DbType.String, state);

using (IDataReader reader = db.ExecuteReader(command))
{ /* read the data */ }

I also recommend reading the documentation that comes with Enterprise Library.  http://msdn.microsoft.com/en-us/library/cc309182.aspx is really quite explicit.

 

hope this helps,

-rks

 

Mar 3, 2010 at 9:47 AM

May be very late reply for you question , i thought of replying because it might be useful to other later .

I used this function just u need to called this function with those paramters , the framework will automatically find out data types of the parameters.

db.GetStoredProcCommand("AddProduct",category,city,state);