Solved

Using stored procedure trough a generic database layer

Posted on 2006-06-21
6
339 Views
Last Modified: 2010-08-05
Hi,

I have a generic database layer which is using IDbCommand, IDbConnection, IDataReader and IDataAdapter, but how do I use stored procedures trough system.data interface?

/Morten
0
Comment
Question by:mrw76
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:Bowmanje
ID: 16951232
Hi Morten,

For classes that implement IDbCommand, set

cmd.CommandText = "sp_name";
cmd.CommandType = CommandType.StoredProcedure;

and then add any parameters to the cmd.Parameters collection.

Finally, use cmd.ExecuteNonQuery(); to make the call.

HTH,
Josh
0
 

Author Comment

by:mrw76
ID: 16951419
Yes but how do I create the parameter list for the command

parms(0) = New SqlClient.SqlParameter("@Res_Id", SqlDbType.NChar, 30)
parms(0).Value = res_id

The parameter above is restricted to the SqlClient, but I want to make it generic.

/Morten
0
 
LVL 3

Accepted Solution

by:
Bowmanje earned 500 total points
ID: 16951804
You can use the OleDb methods...

System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDbCommand();
cmd.CommandText = "sp_name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Res_ID",res_id);

Or, if you are writing your own generic classes, extend the IDbParameter interface.

HTH,
Josh
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 25

Expert Comment

by:dstanley9
ID: 16962170
Try using IDBCommand.CreateParameter():

IDbCommand myCommand // obtained from data layer
IDbDataParameter param = myCommand.CreateParameter();
param.Value = "myvalue";
myCommand.Parameters["@Res_Id"] = param;
0
 

Author Comment

by:mrw76
ID: 16999577
This works, thanks for the help.

IDbDataParameter newParm;           //Stored Procedure parameter

adapter = null;
ds = new DataSet();
connection = this.GetConnection();
command = this.GetCommand(CmdText, connection);
command.CommandType = CommandType.StoredProcedure;

foreach (CommandParameters parm in parms)
{
  newParm = command.CreateParameter();
  newParm.ParameterName = parm.parmName;
  newParm.Direction = parm.parmDirection;
  newParm.Value = parm.parmValue;
  newParm.DbType = parm.parmDBType;
  command.Parameters.Add(newParm);
}
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17000980
I think you accepted the wrong answer.  My solution was to use CreateParameter().
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now