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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How do I communicate with a stored procedure?

How do I communicate with a stored procedure?

Scheduled Pinned Locked Moved C#
csharpdatabasequestionasp-nethelp
5 Posts 3 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.
  • V Offline
    V Offline
    Vernware
    wrote on last edited by
    #1

    I am developing a program using asp.net 2.0. I am using C# as the programming language. In this project I am connected to a SQLServer database (SQLServer 2005). I have created a stored procedure in this database and want to call it from C#, pass it some parameters and receive from it a return value. I am not clear as to how to call it, pass the parameters and then access the return value. I would appreciate any help/advise/suggestions/etc on this. Thanks, Vern Vern

    K E 2 Replies Last reply
    0
    • V Vernware

      I am developing a program using asp.net 2.0. I am using C# as the programming language. In this project I am connected to a SQLServer database (SQLServer 2005). I have created a stored procedure in this database and want to call it from C#, pass it some parameters and receive from it a return value. I am not clear as to how to call it, pass the parameters and then access the return value. I would appreciate any help/advise/suggestions/etc on this. Thanks, Vern Vern

      K Offline
      K Offline
      kryzchek
      wrote on last edited by
      #2

      Use an SqlCommand object: using System.Data; using System.Data.SqlClient; SqlConnection objConnection = null; SqlCommand objCommand = null; //Set up your SqlConnection object objConnection = new SqlConnection(YOUR_CONNECTION_STRING); //Set up your SqlCommand object objCommand = new SqlCommand(); objCommand.CommandType = CommandType.StoredProcedure; objCommand.Connection = objConnection; objCommand.Connection.Open(); //Add in any parameters objCommand.Parameters.Add("@MyParam", MyParamValue); objCommand.ExecuteNonQuery(); if (objCommand.Connection != null) { objCommand.Connection.Close(); objCommand.Connection.Dispose(); } if (objCommand != null) { objCommand.Dispose(); objCommand = null; }

      V 1 Reply Last reply
      0
      • V Vernware

        I am developing a program using asp.net 2.0. I am using C# as the programming language. In this project I am connected to a SQLServer database (SQLServer 2005). I have created a stored procedure in this database and want to call it from C#, pass it some parameters and receive from it a return value. I am not clear as to how to call it, pass the parameters and then access the return value. I would appreciate any help/advise/suggestions/etc on this. Thanks, Vern Vern

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        See using System.Data.SqlClient; Here is some code to play with. If you are returning a query use ExecuteReader instead.

        SqlConnection connection = new SqlConnection("someConnectionString");
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "stored procedure name");

        		SqlParameter parameterCustomerId = command.CreateParameter();
        		parameterCustomerId.DbType = System.Data.DbType.Int64;
        		parameterCustomerId.Direction = System.Data.ParameterDirection.Output;
        		
        		command.ExecuteNonQuery();
        

        I feel so silly, my code doesn't work! parameterCustomerId.ParameterName = "@customerId"; command.Parameters.Add(parameterCustomerId); needs to be added before the execute.


        File Not Found

        V 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          See using System.Data.SqlClient; Here is some code to play with. If you are returning a query use ExecuteReader instead.

          SqlConnection connection = new SqlConnection("someConnectionString");
          SqlCommand command = new SqlCommand();
          command.Connection = connection;
          command.CommandType = System.Data.CommandType.StoredProcedure;
          command.CommandText = "stored procedure name");

          		SqlParameter parameterCustomerId = command.CreateParameter();
          		parameterCustomerId.DbType = System.Data.DbType.Int64;
          		parameterCustomerId.Direction = System.Data.ParameterDirection.Output;
          		
          		command.ExecuteNonQuery();
          

          I feel so silly, my code doesn't work! parameterCustomerId.ParameterName = "@customerId"; command.Parameters.Add(parameterCustomerId); needs to be added before the execute.


          File Not Found

          V Offline
          V Offline
          Vernware
          wrote on last edited by
          #4

          Ennis: Thanks for the tips. This wsa very helpful. Thanks again, Vern

          1 Reply Last reply
          0
          • K kryzchek

            Use an SqlCommand object: using System.Data; using System.Data.SqlClient; SqlConnection objConnection = null; SqlCommand objCommand = null; //Set up your SqlConnection object objConnection = new SqlConnection(YOUR_CONNECTION_STRING); //Set up your SqlCommand object objCommand = new SqlCommand(); objCommand.CommandType = CommandType.StoredProcedure; objCommand.Connection = objConnection; objCommand.Connection.Open(); //Add in any parameters objCommand.Parameters.Add("@MyParam", MyParamValue); objCommand.ExecuteNonQuery(); if (objCommand.Connection != null) { objCommand.Connection.Close(); objCommand.Connection.Dispose(); } if (objCommand != null) { objCommand.Dispose(); objCommand = null; }

            V Offline
            V Offline
            Vernware
            wrote on last edited by
            #5

            kryzchek: Thanks for the code. This code and other code I got from this site was very helpful. Thanks again, Vern

            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