?
Solved

How do you query Stored Procedure Parameters via ADO.net?

Posted on 2009-05-12
3
Medium Priority
?
349 Views
Last Modified: 2013-12-25
I am wondering how to figure out if a store procedure has a certain parameter without a try catch. Basically we have a standard in our database where all of our stored procedures are Delete_<TableName> and a set number of parameters. Well we want to add a @ReturnValue to some but not all right away. Since it is a return value I have to add it to the stored proc because you can't make it optional. So I was wondering if there was a way to check the stored proc to see if @ReturnValue has been defined.

Is that possible?
Dim nCmd As New SqlClient.SqlCommand("Delete_" & TableName)
nCmd.CommandType = CommandType.StoredProcedure
nCmd.Parameters.AddWithValue("@Param1", val1)
nCmd.Parameters.AddWithValue("@Param2", val2)
nCmd.Parameters.AddWithValue("@Param3", val3)
 
'Need logic to check to see if @ReturnValue is on Delete_<TableName>
nCmd.Parameters.Add("@ReturnValue", SqlDbType.VarChar)
nCmd.Parameters("@ReturnValue").Direction = ParameterDirection.Output
nCmd.Parameters("@ReturnValue").Size = 250

Open in new window

0
Comment
Question by:jmarbutt
[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
3 Comments
 
LVL 2

Accepted Solution

by:
jmarbutt earned 0 total points
ID: 24365731
I found the answer via the SQLCommandBuilder

See this article:
http://www.davidhayden.com/blog/dave/archive/2006/11/01/SqlCommandBuilderDeriveParameters.aspx
0
 
LVL 3

Expert Comment

by:GregTSmith
ID: 24365947
All stored procedures have an implicit return value of zero if you do not set it explicitly.  

I'll attach some of the code I was playing with because you might find it useful.

using System;
using System.Data;
using System.Data.SqlClient;
 
public class Database
{
  protected virtual string ConnectionString { get; set; }
 
  public Database(string connectionString)
  {
    this.ConnectionString = connectionString;
  }
 
  public virtual int DeleteTable(string tableName, params object[] parameters)
  {
    int returnValue = 0;
 
    this.UseCommand(
      (command) =>
      {
        command.CommandText = "Delete_" + tableName;
        command.CommandType = CommandType.StoredProcedure;
 
        SqlCommandBuilder.DeriveParameters(command);
 
        for (int i = 0; i < parameters.Length; i++)
          command.Parameters[i].Value = parameters[i];
 
        command.ExecuteNonQuery();
 
        returnValue = (int)command.Parameters[0].Value;
      }
    );
 
    return returnValue;
  }
 
  protected virtual void UseCommand(Action<SqlCommand> lambda)
  {
    this.UseConnection(
      (connection) =>
      {
        using (SqlCommand command = new SqlCommand())
        {
          command.Connection = connection;
          lambda(command);
        }
      }
    );
  }
 
  protected virtual void UseConnection(Action<SqlConnection> lambda)
  {
    using (SqlConnection connection = new SqlConnection(this.ConnectionString))
    {
      connection.Open();
      lambda(connection);
      connection.Close();
    }
  }
}

Open in new window

0
 
LVL 3

Expert Comment

by:GregTSmith
ID: 24365969
Oops... that code won't quite work... since all procedures have a return value, you'd have to offset the parameter value you're setting by 1...  ignore the last code posting and look at this one instead.
using System;
using System.Data;
using System.Data.SqlClient;
 
public class Database
{
  protected virtual string ConnectionString { get; set; }
 
  public Database(string connectionString)
  {
    this.ConnectionString = connectionString;
  }
 
  public virtual int DeleteTable(string tableName, params object[] parameters)
  {
    int returnValue = 0;
 
    this.UseCommand(
      (command) =>
      {
        command.CommandText = "Delete_" + tableName;
        command.CommandType = CommandType.StoredProcedure;
 
        SqlCommandBuilder.DeriveParameters(command);
 
        for (int i = 0; i < parameters.Length; i++)
          command.Parameters[i + 1].Value = parameters[i];
 
        command.ExecuteNonQuery();
 
        returnValue = (int)command.Parameters[0].Value;
      }
    );
 
    return returnValue;
  }
 
  protected virtual void UseCommand(Action<SqlCommand> lambda)
  {
    this.UseConnection(
      (connection) =>
      {
        using (SqlCommand command = new SqlCommand())
        {
          command.Connection = connection;
          lambda(command);
        }
      }
    );
  }
 
  protected virtual void UseConnection(Action<SqlConnection> lambda)
  {
    using (SqlConnection connection = new SqlConnection(this.ConnectionString))
    {
      connection.Open();
      lambda(connection);
      connection.Close();
    }
  }
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 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