Solved

Using stored procedure trough a generic database layer

Posted on 2006-06-21
6
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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