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. running the Procedure

running the Procedure

Scheduled Pinned Locked Moved C#
helptutorial
9 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.
  • T Offline
    T Offline
    tasumisra
    wrote on last edited by
    #1

    Hi all, Can anybody help me here ... how to run a procedure with outparameter... please provide the code if u can... Thanks alot

    T@SU

    Mircea PuiuM J 2 Replies Last reply
    0
    • T tasumisra

      Hi all, Can anybody help me here ... how to run a procedure with outparameter... please provide the code if u can... Thanks alot

      T@SU

      Mircea PuiuM Offline
      Mircea PuiuM Offline
      Mircea Puiu
      wrote on last edited by
      #2

      using (SqlConnection sc = new SqlConnection())
      {
      StringBuilder sql = new StringBuilder(); // or simply use string instead
      sql.Append("exec sp_YourProc");
      string connStr = "...";
      sc.ConnectionString = connStr;
      sc.Open();
      using (SqlCommand sco = new SqlCommand())
      {
      try
      {
      sco.CommandType = CommandType.Text;
      sco.CommandText = sql.ToString();
      sco.Connection = sc;
      using (SqlDataReader sr = sco.ExecuteReader())
      {
      // Do something with the incoming data;
      }
      }
      catch (SqlException exc)
      {
      // ... Deal with exceptions
      }
      }

      SkyWalker

      J 1 Reply Last reply
      0
      • T tasumisra

        Hi all, Can anybody help me here ... how to run a procedure with outparameter... please provide the code if u can... Thanks alot

        T@SU

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Same as any normal procedure, just make sure you add the output parameter and give it a Direction of Output (or InputOutput) SqlParameter outParam = new SqlParameter("@myOutParam",SqlDbType.Int); outParam.Direction = ParameterDirection.Output; myCmd.Parameters.Add(outParam);

        T 1 Reply Last reply
        0
        • Mircea PuiuM Mircea Puiu

          using (SqlConnection sc = new SqlConnection())
          {
          StringBuilder sql = new StringBuilder(); // or simply use string instead
          sql.Append("exec sp_YourProc");
          string connStr = "...";
          sc.ConnectionString = connStr;
          sc.Open();
          using (SqlCommand sco = new SqlCommand())
          {
          try
          {
          sco.CommandType = CommandType.Text;
          sco.CommandText = sql.ToString();
          sco.Connection = sc;
          using (SqlDataReader sr = sco.ExecuteReader())
          {
          // Do something with the incoming data;
          }
          }
          catch (SqlException exc)
          {
          // ... Deal with exceptions
          }
          }

          SkyWalker

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Where's the output param the OP asked about?

          Mircea PuiuM 1 Reply Last reply
          0
          • J J4amieC

            Where's the output param the OP asked about?

            Mircea PuiuM Offline
            Mircea PuiuM Offline
            Mircea Puiu
            wrote on last edited by
            #5

            use sco.Parameters.Add()

            SkyWalker

            1 Reply Last reply
            0
            • J J4amieC

              Same as any normal procedure, just make sure you add the output parameter and give it a Direction of Output (or InputOutput) SqlParameter outParam = new SqlParameter("@myOutParam",SqlDbType.Int); outParam.Direction = ParameterDirection.Output; myCmd.Parameters.Add(outParam);

              T Offline
              T Offline
              tasumisra
              wrote on last edited by
              #6

              Thanks alot for ur replies.... after executing the procedure ... how to retrive the result.. am trying like this but it dosent work.. dataset abc=cmd.ExecuteNonQuery(); i have done like this..... SqlConnection conn = new SqlConnection(connc); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_abc"; SqlParameter paramPAF = new SqlParameter(); paramPAF.ParameterName = "P_Id"; paramPAF.SqlDbType = SqlDbType.Int; paramPAF.Value = Section_Id; paramPAF.Direction = ParameterDirection.Input; SqlParameter paramNbr = new SqlParameter(); paramNbr.ParameterName = "ResultRS"; paramNbr.SqlDbType = SqlDbType.Variant; paramNbr.Direction = ParameterDirection.Output; cmd.Parameters.Add(P_Id); // parameter 1 cmd.Parameters.Add(sp_abc); cmd.ExecuteNonQuery(); so now how to use third parameter "ResultRS" ? Please help me here..

              vikas da

              S 1 Reply Last reply
              0
              • T tasumisra

                Thanks alot for ur replies.... after executing the procedure ... how to retrive the result.. am trying like this but it dosent work.. dataset abc=cmd.ExecuteNonQuery(); i have done like this..... SqlConnection conn = new SqlConnection(connc); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_abc"; SqlParameter paramPAF = new SqlParameter(); paramPAF.ParameterName = "P_Id"; paramPAF.SqlDbType = SqlDbType.Int; paramPAF.Value = Section_Id; paramPAF.Direction = ParameterDirection.Input; SqlParameter paramNbr = new SqlParameter(); paramNbr.ParameterName = "ResultRS"; paramNbr.SqlDbType = SqlDbType.Variant; paramNbr.Direction = ParameterDirection.Output; cmd.Parameters.Add(P_Id); // parameter 1 cmd.Parameters.Add(sp_abc); cmd.ExecuteNonQuery(); so now how to use third parameter "ResultRS" ? Please help me here..

                vikas da

                S Offline
                S Offline
                Steppo
                wrote on last edited by
                #7

                You have to use the cmd.ExecuteReader() and not the cmd.ExecuteNonQuery() - the last method return an int of numbers of rows affected, usually used with insert query or update query (also useful with select count...). Try this: SqlDataReader reader = cmd.ExecuteReader(); reader.Close(); string result = reader["ResultRS"].ToString();

                T 1 Reply Last reply
                0
                • S Steppo

                  You have to use the cmd.ExecuteReader() and not the cmd.ExecuteNonQuery() - the last method return an int of numbers of rows affected, usually used with insert query or update query (also useful with select count...). Try this: SqlDataReader reader = cmd.ExecuteReader(); reader.Close(); string result = reader["ResultRS"].ToString();

                  T Offline
                  T Offline
                  tasumisra
                  wrote on last edited by
                  #8

                  it is showinf error like "Procedure or function has too many arguments specified". i am using "paramNbr.SqlDbType = SqlDbType.Variant;" in case out parameter sqldbtype is that fine..

                  T@SU

                  S 1 Reply Last reply
                  0
                  • T tasumisra

                    it is showinf error like "Procedure or function has too many arguments specified". i am using "paramNbr.SqlDbType = SqlDbType.Variant;" in case out parameter sqldbtype is that fine..

                    T@SU

                    S Offline
                    S Offline
                    Steppo
                    wrote on last edited by
                    #9

                    SqlConnection conn = new SqlConnection("CONNECTION HERE"); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_abc"; //Create the first parameter SqlParameter firstParam = new SqlParameter("P_Id", SqlDbType.Int); firstParam.Value = 0; //Place your variable firstParam.Direction = ParameterDirection.Input; cmd.Parameters.Add(firstParam); //Parameter 1 //Create the second parameter SqlParameter secondParam = new SqlParameter("ResultRS", SqlDbType.Variant); secondParam.Direction = ParameterDirection.Output; cmd.Parameters.Add(secondParam); //Parameter 2 int count = cmd.ExecuteNonQuery(); Console.WriteLine(secondParam.Value.ToString()); This is correct now, secondParam.Value and you read the value News at http://support.microsoft.com/kb/308621[^]

                    modified on Monday, January 07, 2008 8:36:36 AM

                    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