Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:56:49+00:00 2026-06-01T15:56:49+00:00

I am trying to create a generic method for calling stored procedures I would

  • 0

I am trying to create a generic method for calling stored procedures

I would like to pass in the Parameters in via an array

At the moment i am having trouble adding the parameters to the SqlCommand

This is what i have so far

Can anyone advise

thanks

Simon

Calling the method

string[] paramNames = new string[1];
paramNames[0] = "@date = 2012-1-1";
string err="";


WriteToDatabase("exec LoadData", CommandType.StoredProcedure, paramNames, out err);

Method

public static bool WriteToDatabase(
        string sql,
        CommandType commandType,
        string[] paramNames,
        out string errorText)
    {
        bool success = false;
        errorText = "";
        try
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
               connection.Open(); 
                List<SqlParameter> parameters = new List<SqlParameter>();

                foreach (string paramName in paramNames)
                {
                    parameters.Add(new SqlParameter() { ParameterName = paramName });
                }

                using (SqlCommand command = new SqlCommand()
                {
                    Connection = connection,
                    CommandText = sql,
                    CommandType = commandType,
                    Parameters = parameters

                })
                 command.ExecuteNonQuery();  

                 connection.Close();
            }


        }
        catch (SqlException sex)
        {
            log.Error("QueryDatabase SQLexception:" + sex.Message);
        }
        catch (Exception ex)
        {
            log.Error("QueryDatabase exception:" + ex.Message);
        }
        return success;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:56:51+00:00Added an answer on June 1, 2026 at 3:56 pm

    Here’s a class I whipped up a while back. It’s pretty easy to use:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Xml;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    using System.Text;
    
    namespace NESCTC.Data
    {   
        public class DataAccess : IDisposable
        {
            #region declarations
    
            private SqlCommand _cmd;
            private string _SqlConnString;
    
            #endregion
    
            #region constructors
    
            public DataAccess(string ConnectionString)
            {
                _cmd = new SqlCommand();
                _cmd.CommandTimeout = 240;
                _SqlConnString = ConnectionString;
            }
    
            #endregion
    
            #region IDisposable implementation
    
            ~DataAccess()
            {
                Dispose(false);
            }
    
            public void Dispose()
            {
                Dispose(true);            
            }
    
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _cmd.Connection.Dispose();
                    _cmd.Dispose();
                }
            }
    
            #endregion
    
            #region data retrieval methods
    
            public DataTable ExecReturnDataTable()
            {
                using (var conn = new SqlConnection(this.ConnectionString))
                {
                    try
                    {
                        PrepareCommandForExecution(conn);
                        using (SqlDataAdapter adap = new SqlDataAdapter(_cmd))
                        {
                            DataTable dt = new DataTable();
                            adap.Fill(dt);
                            return dt;
                        }
                    }
                    finally
                    {
                        _cmd.Connection.Close();
                    }
                }
            }                
    
            public object ExecScalar()
            {
                using (var conn = new SqlConnection(this.ConnectionString))
                {
                    try
                    {
                        PrepareCommandForExecution(conn);
                        return _cmd.ExecuteScalar();
                    }
                    finally
                    {
                        _cmd.Connection.Close();
                    }
                }
            }    
    
            #endregion
    
            #region data insert and update methods
    
            public void ExecNonQuery()
            {
                using (var conn = new SqlConnection(this.ConnectionString))
                {
                    try
                    {
                        PrepareCommandForExecution(conn);
                        _cmd.ExecuteNonQuery();
                    }
                    finally
                    {
                        _cmd.Connection.Close();
                    }
                }
            }
    
            #endregion
    
            #region helper methods
    
            public void AddParm(string ParameterName, SqlDbType ParameterType, object Value)
            { _cmd.Parameters.Add(ParameterName, ParameterType).Value = Value; }
    
            private SqlCommand PrepareCommandForExecution(SqlConnection conn)
            {
                try
                {
                    _cmd.Connection = conn;
                    _cmd.CommandType = CommandType.StoredProcedure;
                    _cmd.CommandTimeout = this.CommandTimeout;
                    _cmd.Connection.Open();
    
                    return _cmd;
                }
                finally
                {
                    _cmd.Connection.Close();
                }
            }
    
            #endregion
    
            #region properties
    
            public int CommandTimeout
            {
                get { return _cmd.CommandTimeout; }
                set { _cmd.CommandTimeout = value; }
            }
    
            public string ProcedureName
            {
                get { return _cmd.CommandText; }
                set { _cmd.CommandText = value; }
            }
    
            public string ConnectionString
            {
                get { return _SqlConnString; }
                set { _SqlConnString = value; }
            }
    
            #endregion
        }
    }
    

    Here is an example of how to use it:

    public void UpdateWorkOrder(int workOrderID, int paymentTermTypeID, string acceptedBy, string lastIssuedBy)
    {
        using (var data = new DataAccess(this.ConnectionString))
        {
            data.ProcedureName = "UpdateWorkOrderDetails";
            data.AddParm("@WorkOrderID", SqlDbType.Int, workOrderID);
            data.AddParm("@PaymentTermTypeID", SqlDbType.Int, paymentTermTypeID);
            data.AddParm("@AcceptedBy", SqlDbType.VarChar, acceptedBy);
            data.AddParm("@LastIssuedBy", SqlDbType.VarChar, lastIssuedBy);
            data.ExecNonQuery();
        }
    }
    
    public DataTable GetWorkOrder(int workOrderID)
    {
        using (var data = new DataAccess(this.ConnectionString))
        {
            data.ProcedureName = "GetWorkOrder";
            data.AddParm("@WorkOrderID", SqlDbType.Int, workOrderID);
            return data.ExecReturnDataTable();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a generic method that will read an attribute on
I am trying to create an extension method for the generic delegate Action<T> to
I'm trying to create a generic build template for my Makefiles, kind of like
I've been trying to create a generic event. Basically it should look like this:
I am trying to create a generic method using EF4 to find the primary
I'm trying to create a generic method to use in my base class for
I am trying to create a generic method that will retrieve an item by
So I'm trying to create a generic select by ID method for a base
I'm trying to create a generic method that will return a predicate to find
I'm trying to create a generic method to cast an object, but can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.