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. Parameterized Queries

Parameterized Queries

Scheduled Pinned Locked Moved C#
csharpdatabasevisual-studiotutorialquestion
5 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.
  • B Offline
    B Offline
    BBatts
    wrote on last edited by
    #1

    Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#. Also, is there any real advantage to stored procedures vs parameterized queries? For Example:

    using (SqlConnection dbConnection = new SqlConnection(GSettings.SQLConnectionString))
    {
    dbConnection.Open();

                for (int i = 0; i < DBUpdateList.Count; i++)
                {
                    string selectSQL = "BEGIN TRANSACTION; UPDATE TABLE SET Dog = [@Dog](/Members/dog); COMMIT;";
    
                    using (SqlCommand dbCommand = new SqlCommand(selectSQL, dbConnection))
                    {
                        dbCommand.Parameters.AddWithValue("[@Dog](/Members/dog)", "Poodle");
                        dbCommand.ExecuteNonQuery();
                    }
                }
            }
    
    S P L 3 Replies Last reply
    0
    • B BBatts

      Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#. Also, is there any real advantage to stored procedures vs parameterized queries? For Example:

      using (SqlConnection dbConnection = new SqlConnection(GSettings.SQLConnectionString))
      {
      dbConnection.Open();

                  for (int i = 0; i < DBUpdateList.Count; i++)
                  {
                      string selectSQL = "BEGIN TRANSACTION; UPDATE TABLE SET Dog = [@Dog](/Members/dog); COMMIT;";
      
                      using (SqlCommand dbCommand = new SqlCommand(selectSQL, dbConnection))
                      {
                          dbCommand.Parameters.AddWithValue("[@Dog](/Members/dog)", "Poodle");
                          dbCommand.ExecuteNonQuery();
                      }
                  }
              }
      
      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      Personally I would only use BEGIN TRANSACTION; END in a stored procedure. If you want to use transactions with your ADO.NET query I would read this MSDN transactionscope[^]

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      1 Reply Last reply
      0
      • B BBatts

        Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#. Also, is there any real advantage to stored procedures vs parameterized queries? For Example:

        using (SqlConnection dbConnection = new SqlConnection(GSettings.SQLConnectionString))
        {
        dbConnection.Open();

                    for (int i = 0; i < DBUpdateList.Count; i++)
                    {
                        string selectSQL = "BEGIN TRANSACTION; UPDATE TABLE SET Dog = [@Dog](/Members/dog); COMMIT;";
        
                        using (SqlCommand dbCommand = new SqlCommand(selectSQL, dbConnection))
                        {
                            dbCommand.Parameters.AddWithValue("[@Dog](/Members/dog)", "Poodle");
                            dbCommand.ExecuteNonQuery();
                        }
                    }
                }
        
        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        BBatts wrote:

        Is there any benefit

        No.

        BBatts wrote:

        stored procedures vs parameterized queries

        Those aren't mutually exclusive; even if you use stored procedures, you should still use a parameterized command to provide values (as necessary). I also recommend that you not keep reinstantiating the Command, but reuse one:

        instantiate command
        add parameter(s)
        (begin transaction if desired)
        loop:
        set Value(s)
        Execute
        (commit transaction)

        1 Reply Last reply
        0
        • B BBatts

          Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#. Also, is there any real advantage to stored procedures vs parameterized queries? For Example:

          using (SqlConnection dbConnection = new SqlConnection(GSettings.SQLConnectionString))
          {
          dbConnection.Open();

                      for (int i = 0; i < DBUpdateList.Count; i++)
                      {
                          string selectSQL = "BEGIN TRANSACTION; UPDATE TABLE SET Dog = [@Dog](/Members/dog); COMMIT;";
          
                          using (SqlCommand dbCommand = new SqlCommand(selectSQL, dbConnection))
                          {
                              dbCommand.Parameters.AddWithValue("[@Dog](/Members/dog)", "Poodle");
                              dbCommand.ExecuteNonQuery();
                          }
                      }
                  }
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          BBatts wrote:

          Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#

          I'd recommend getting a SQL manual, and that's not meant as a snarky remark; programming often involves databases, and you'll need to dig into transactions sooner or later. Life also becomes easier when you can look up what each statement does :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          B 1 Reply Last reply
          0
          • L Lost User

            BBatts wrote:

            Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#

            I'd recommend getting a SQL manual, and that's not meant as a snarky remark; programming often involves databases, and you'll need to dig into transactions sooner or later. Life also becomes easier when you can look up what each statement does :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            B Offline
            B Offline
            BBatts
            wrote on last edited by
            #5

            Got one you would recommended?

            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