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. How to create stored procedure through c# at runtime?

How to create stored procedure through c# at runtime?

Scheduled Pinned Locked Moved C#
csharptutorialdatabasebusinesshelp
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.
  • S Offline
    S Offline
    S a n d y
    wrote on last edited by
    #1

    Hello, How to create stored procedure dynamically in c#.net. Because some of my requirements needs such a situation. Please help with some example code. Thanks and Regards

    N 1 Reply Last reply
    0
    • S S a n d y

      Hello, How to create stored procedure dynamically in c#.net. Because some of my requirements needs such a situation. Please help with some example code. Thanks and Regards

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Just execute the statements as you did for normal query. But why you need to create a SP run time? That doesn't make any sense.

      Navaneeth How to use google | Ask smart questions

      S G 2 Replies Last reply
      0
      • N N a v a n e e t h

        Just execute the statements as you did for normal query. But why you need to create a SP run time? That doesn't make any sense.

        Navaneeth How to use google | Ask smart questions

        S Offline
        S Offline
        S a n d y
        wrote on last edited by
        #3

        Hi Navaneeth, Because of requirement, I need to create tables and stored procedures at runtime. This is created at runtime because it is managed on clients system or database server during installation of application. Thanks for reply.

        N 1 Reply Last reply
        0
        • N N a v a n e e t h

          Just execute the statements as you did for normal query. But why you need to create a SP run time? That doesn't make any sense.

          Navaneeth How to use google | Ask smart questions

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          N a v a n e e t h wrote:

          But why you need to create a SP run time? That doesn't make any sense.

          He might be coding and OR mapper :)

          Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

          S 1 Reply Last reply
          0
          • G Giorgi Dalakishvili

            N a v a n e e t h wrote:

            But why you need to create a SP run time? That doesn't make any sense.

            He might be coding and OR mapper :)

            Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

            S Offline
            S Offline
            S a n d y
            wrote on last edited by
            #5

            Then, what i do? Can i take backup of that and the giving it for restore.

            1 Reply Last reply
            0
            • S S a n d y

              Hi Navaneeth, Because of requirement, I need to create tables and stored procedures at runtime. This is created at runtime because it is managed on clients system or database server during installation of application. Thanks for reply.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Well, Try the following code

              string query = @"CREATE PROCEDURE SampleProcedure
              AS
              BEGIN
              SELECT 'From Sample Procedure'
              END";
              using (SqlConnection connection = new SqlConnection(@"YourConnectionString"))
              using (SqlCommand command = new SqlCommand(query,connection))
              {
              connection.Open();
              command.ExecuteNonQuery();
              }

              If you have many SQL statements to be executed, I'd put it in a file and read from there to execute.

              Navaneeth How to use google | Ask smart questions

              S 1 Reply Last reply
              0
              • N N a v a n e e t h

                Well, Try the following code

                string query = @"CREATE PROCEDURE SampleProcedure
                AS
                BEGIN
                SELECT 'From Sample Procedure'
                END";
                using (SqlConnection connection = new SqlConnection(@"YourConnectionString"))
                using (SqlCommand command = new SqlCommand(query,connection))
                {
                connection.Open();
                command.ExecuteNonQuery();
                }

                If you have many SQL statements to be executed, I'd put it in a file and read from there to execute.

                Navaneeth How to use google | Ask smart questions

                S Offline
                S Offline
                S a n d y
                wrote on last edited by
                #7

                Could you please explain? What is the problem in this create procedure QueryString. "CREATE PROCEDURE test.dbo.Operator @MasterField varchar(300) AS SELECT SubOperatorField FROM SubOperatorInfo WHERE MasterField=@MasterField AND departField IS NOT NULL AND SubOperatorField=somevalue"; this querystring fires an exception i.e.:- 'CREATE PROCEDURE' does not allow specifying the database name as a prefix to the object name. Must declare the variable '@MasterField'.

                J 1 Reply Last reply
                0
                • S S a n d y

                  Could you please explain? What is the problem in this create procedure QueryString. "CREATE PROCEDURE test.dbo.Operator @MasterField varchar(300) AS SELECT SubOperatorField FROM SubOperatorInfo WHERE MasterField=@MasterField AND departField IS NOT NULL AND SubOperatorField=somevalue"; this querystring fires an exception i.e.:- 'CREATE PROCEDURE' does not allow specifying the database name as a prefix to the object name. Must declare the variable '@MasterField'.

                  J Offline
                  J Offline
                  James Simpson
                  wrote on last edited by
                  #8

                  try taking the database name out of the name of the procedure You should be already in the context of the database, e.g the SqlConnection is already connected to "test". James

                  James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
                  Mitch Hedberg

                  S 1 Reply Last reply
                  0
                  • J James Simpson

                    try taking the database name out of the name of the procedure You should be already in the context of the database, e.g the SqlConnection is already connected to "test". James

                    James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
                    Mitch Hedberg

                    S Offline
                    S Offline
                    S a n d y
                    wrote on last edited by
                    #9

                    Thank you very much. This works.

                    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