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. getting a dataset of affected records,using an ExecuteNONQuery OleDbCommand

getting a dataset of affected records,using an ExecuteNONQuery OleDbCommand

Scheduled Pinned Locked Moved C#
question
8 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.
  • D Offline
    D Offline
    dsovino
    wrote on last edited by
    #1

    Hi to everyone! I need to get the autonumeric field assigned to a new record, because I want to use it on a different table. ExecuteNonQuery retrieves the number of affected rows. But how can I get a dataset result of the affected rows, as the one using an ExecuteQuery command? Thanks for everyones time! :doh: daniel sovino

    G 1 Reply Last reply
    0
    • D dsovino

      Hi to everyone! I need to get the autonumeric field assigned to a new record, because I want to use it on a different table. ExecuteNonQuery retrieves the number of affected rows. But how can I get a dataset result of the affected rows, as the one using an ExecuteQuery command? Thanks for everyones time! :doh: daniel sovino

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can ask he database for the last created key. Depending on what database you are using, you use different functions: MySQL: select last_insert_id() MS SQL: select scope_identity() Access: select @@identity

      --- Year happy = new Year(2007);

      A D 2 Replies Last reply
      0
      • G Guffa

        You can ask he database for the last created key. Depending on what database you are using, you use different functions: MySQL: select last_insert_id() MS SQL: select scope_identity() Access: select @@identity

        --- Year happy = new Year(2007);

        A Offline
        A Offline
        aSarafian
        wrote on last edited by
        #3

        Question. If the affected Rows are more than 1 do the above statements return one ID or more?

        G 1 Reply Last reply
        0
        • G Guffa

          You can ask he database for the last created key. Depending on what database you are using, you use different functions: MySQL: select last_insert_id() MS SQL: select scope_identity() Access: select @@identity

          --- Year happy = new Year(2007);

          D Offline
          D Offline
          dsovino
          wrote on last edited by
          #4

          Ok, but how do I get this done? I had lots of troubles working with access database and the dataset wizard to store querys (http://forums.microsoft.com/MSDN/showpost.aspx?postid=1121807&siteid=1), so I opted to run my queries with code. I used one method with the Insert command, and if the result set is higher than 0, I call another method with a Scalar Command, to search for the ID. Then, with the retrieved ID, i call the third method to Insert the record ID on the second table.... :zzz: The code described below worked well, everyonce in a while (no clue yet why sometimes i'm able to get the ID and sometimes I get 0, probably something going on with the query). So that's why I want a safer way to get this done. I searched a little bit for "select @@identity" (http://support.microsoft.com/kb/816112), and I found out that it behaves as a Scalar command as well. So in theory, i should just change my query string to get it done. It doesnt work at all. Does the "select @@identity" statement work only when you handle it on a OleDbDataAdapter.RowUpdated Event? Thanks for the help! string file = "......"; private void InsertAudio() { // creation of queries, source, connections, etc.... OleDbCommand cmd = new OleDbCommand(sql, conexion); int resultSet; try { conexion.Open(); resultSet = cmd.ExecuteNonQuery(); if (resultSet > 0) SearchIDofCreatedRow(); } catch (OleDbException ode) { MessageBox.Show(ode.ToString(), "Error de Conexion con base de datos"); } finally {conexion.Close();} } private void SearchIDofCreatedRow() { // creation of queries, source, connections, etc.... OleDbCommand cmd = new OleDbCommand("SELECT ID FROM audios WHERE file='" + file + "'", conexion); try { conexion.Open(); int ID = Convert.ToInt32(cmd.ExecuteScalar()); AddCategoryList(ID); } //catch/finally..... } private void AddCategoryList(int ID) { // //Insert into the second table with the retrieved data.... // }

          daniel sovino

          G 1 Reply Last reply
          0
          • A aSarafian

            Question. If the affected Rows are more than 1 do the above statements return one ID or more?

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            It only returns the last id.

            --- Year happy = new Year(2007);

            A 1 Reply Last reply
            0
            • D dsovino

              Ok, but how do I get this done? I had lots of troubles working with access database and the dataset wizard to store querys (http://forums.microsoft.com/MSDN/showpost.aspx?postid=1121807&siteid=1), so I opted to run my queries with code. I used one method with the Insert command, and if the result set is higher than 0, I call another method with a Scalar Command, to search for the ID. Then, with the retrieved ID, i call the third method to Insert the record ID on the second table.... :zzz: The code described below worked well, everyonce in a while (no clue yet why sometimes i'm able to get the ID and sometimes I get 0, probably something going on with the query). So that's why I want a safer way to get this done. I searched a little bit for "select @@identity" (http://support.microsoft.com/kb/816112), and I found out that it behaves as a Scalar command as well. So in theory, i should just change my query string to get it done. It doesnt work at all. Does the "select @@identity" statement work only when you handle it on a OleDbDataAdapter.RowUpdated Event? Thanks for the help! string file = "......"; private void InsertAudio() { // creation of queries, source, connections, etc.... OleDbCommand cmd = new OleDbCommand(sql, conexion); int resultSet; try { conexion.Open(); resultSet = cmd.ExecuteNonQuery(); if (resultSet > 0) SearchIDofCreatedRow(); } catch (OleDbException ode) { MessageBox.Show(ode.ToString(), "Error de Conexion con base de datos"); } finally {conexion.Close();} } private void SearchIDofCreatedRow() { // creation of queries, source, connections, etc.... OleDbCommand cmd = new OleDbCommand("SELECT ID FROM audios WHERE file='" + file + "'", conexion); try { conexion.Open(); int ID = Convert.ToInt32(cmd.ExecuteScalar()); AddCategoryList(ID); } //catch/finally..... } private void AddCategoryList(int ID) { // //Insert into the second table with the retrieved data.... // }

              daniel sovino

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              The @@identity variable is local to the database session, and if you create another connection you get a new database session. You have to get the id using the same database connection that you used to insert the record. Why do you create a new connection in SearchIDofCreatedRow? When you call it you already have an open connection, pass that along in the call.

              --- Year happy = new Year(2007);

              1 Reply Last reply
              0
              • G Guffa

                It only returns the last id.

                --- Year happy = new Year(2007);

                A Offline
                A Offline
                aSarafian
                wrote on last edited by
                #7

                The original question said about rows. The only way I can think except from triggers is to have a column something like just updated. Then query for those and just before finishing set the columns to false.

                G 1 Reply Last reply
                0
                • A aSarafian

                  The original question said about rows. The only way I can think except from triggers is to have a column something like just updated. Then query for those and just before finishing set the columns to false.

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Sarafian wrote:

                  The original question said about rows.

                  Well, you don't always get what you ask for. :)

                  Sarafian wrote:

                  The only way I can think except from triggers is to have a column something like just updated. Then query for those and just before finishing set the columns to false.

                  An alternative would be to first get the id:s of the records to update, then use them to update the records.

                  --- Year happy = new Year(2007);

                  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