Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# calling Stored Procedures

c# calling Stored Procedures

Scheduled Pinned Locked Moved C#
csharpdatabasedata-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi All 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 parameters = new List();

                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(); 
            } 
    
    
        }
    
    P J P 3 Replies Last reply
    0
    • S si_69

      Hi All 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 parameters = new List();

                  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(); 
              } 
      
      
          }
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Well, you have supplied as the value for paramName @date = 2012-1-1. That's not a parameter name, that's a parameter name and associated value. Also, what type is your parameter?

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      1 Reply Last reply
      0
      • S si_69

        Hi All 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 parameters = new List();

                    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(); 
                } 
        
        
            }
        
        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        si_69 wrote:

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

        Normally that isn't a good idea. It adds complexity and does not have a significant impact on maintenance costs.

        si_69 wrote:

        Can anyone advise

        So if you still want to do it... 1. You must create a sql statement dynamically with matching parameter place holders corresponding to those in the collection(array.) So you are creating SQL dynamically. 2. You must then create a SqlCommand with at sql. Then you populate it the parameters with the collection.

        1 Reply Last reply
        0
        • S si_69

          Hi All 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 parameters = new List();

                      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(); 
                  } 
          
          
              }
          
          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4
          1. It probably shouldn't be static. 1) You should probably not keep creating the Connection and Command -- create one and hold onto it (in the closed state). 2) Where does ConnectionString come from? 3) Here's your code with a few changes. It compiles, but it's untested:

            public static bool WriteToDatabase(
            string sql,
            System.Data.CommandType commandType,
            out string errorText,
            params System.Tuple<string,object>[] values
            )
            {
            bool success = true;
            errorText = null;

             using (System.Data.IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionString)) 
             { 
               try 
               {                        
                 using (System.Data.IDbCommand command = connection.CreateCommand() )
                 { 
                   command.CommandText = sql ;
                   command.CommandType = commandType ;
            
                   foreach (System.Tuple<string,object> param in values ) 
                   {                  
                     System.Data.IDbDataParameter p = command.CreateParameter() ;
                     p.ParameterName = param.Item1 ;
                     p.Value = param.Item2 ;
                     command.Parameters.Add(p); 
                   } 
            
                  connection.Open();  
                  command.ExecuteNonQuery();   
                 } 
               } 
               catch ( System.Exception err )
               {
                 success = false ;
                 errorText = err.ToString() ;
               }                             
               finally
               {
                  connection.Close(); 
               }
             } 
               
             return ( success ) ;
            

            }

             WriteToDatabase
             (
               "blah blah blah" 
             , 
               System.Data.CommandType.Text 
             ,  
               out error
             ,
               new System.Tuple<string,object> ( "@date" , System.DateTime.Now ) 
             ) ;
            
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups