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. Delete command with DataSet and DataAdapter

Delete command with DataSet and DataAdapter

Scheduled Pinned Locked Moved C#
databasehelpquestionannouncement
7 Posts 2 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
    Stefan R
    wrote on last edited by
    #1

    Good evening ( although it's night time in Sweden) I'm trying to delete all my records in a single table with a DataAdapter. The code I use is this: odbAdapter = new OleDbDataAdapter(); odbAdapter.SelectCommand = new OleDbCommand("Select * from Bilder", odbConnection); OleDbCommandBuilder cb = new OleDbCommandBuilder(odbAdapter); odbConnection.Open(); odbAdapter.Fill(ds, "Bilder"); DataTable dt = ds.Tables["Bilder"]; DataRow[] dr = dt.Select(); foreach(DataRow item in dr) { item.Delete(); } odbAdapter.DeleteCommand = cb.GetDeleteCommand(); odbAdapter.Update(ds,"Bilder"); However, I get an error message saying : Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information I'm guessing this has something to do with a primary key or something, judging by info gathered at other places. The primary key in the table is not automatically generated or anything. What the heck am i doing wrong here?

    D 2 Replies Last reply
    0
    • S Stefan R

      Good evening ( although it's night time in Sweden) I'm trying to delete all my records in a single table with a DataAdapter. The code I use is this: odbAdapter = new OleDbDataAdapter(); odbAdapter.SelectCommand = new OleDbCommand("Select * from Bilder", odbConnection); OleDbCommandBuilder cb = new OleDbCommandBuilder(odbAdapter); odbConnection.Open(); odbAdapter.Fill(ds, "Bilder"); DataTable dt = ds.Tables["Bilder"]; DataRow[] dr = dt.Select(); foreach(DataRow item in dr) { item.Delete(); } odbAdapter.DeleteCommand = cb.GetDeleteCommand(); odbAdapter.Update(ds,"Bilder"); However, I get an error message saying : Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information I'm guessing this has something to do with a primary key or something, judging by info gathered at other places. The primary key in the table is not automatically generated or anything. What the heck am i doing wrong here?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Why are you using a DataAdapter for this? If all you want to do is delete all the records in a table, why have the database waste the time retrieving them all and send them to you, just so you can tag them all for delete (one at a time, very time consuming!), then have the Adapter run a delete command on them (one at a time again!)? Just tell the database to delete them all yourself!

      string sql = "DELETE Bilder";
      OleDbConnection conn = new OleDbConnection(_connectionString_);
      OleDbCommand comm = new OleDbCommand(sql, conn);
      conn.Open();
      comm.ExecuteNonQuery();
      conn.Close();
      

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 21:55 Thursday 22nd December, 2005

      1 Reply Last reply
      0
      • S Stefan R

        Good evening ( although it's night time in Sweden) I'm trying to delete all my records in a single table with a DataAdapter. The code I use is this: odbAdapter = new OleDbDataAdapter(); odbAdapter.SelectCommand = new OleDbCommand("Select * from Bilder", odbConnection); OleDbCommandBuilder cb = new OleDbCommandBuilder(odbAdapter); odbConnection.Open(); odbAdapter.Fill(ds, "Bilder"); DataTable dt = ds.Tables["Bilder"]; DataRow[] dr = dt.Select(); foreach(DataRow item in dr) { item.Delete(); } odbAdapter.DeleteCommand = cb.GetDeleteCommand(); odbAdapter.Update(ds,"Bilder"); However, I get an error message saying : Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information I'm guessing this has something to do with a primary key or something, judging by info gathered at other places. The primary key in the table is not automatically generated or anything. What the heck am i doing wrong here?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Stefan R wrote:

        Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information

        Since your SELECT command is returning all the columns of the Bilder table, this message means that your table doesn't have a Primary Key. DataAdapters will only work with tables that define a Primary Key. This is because changes that are made you the DataSet are written back to the database one record at a time. The DataAdapter looks at each record in the DataSet and determines which SQL Command to execute against that record in order to properly update the database, wheather it be an INSERT, UPDATE, or DELETE command. Now, if the DataAdapter is making changes to the tables in the DataSet one record at a time, how does it know how to tell the database which record to change? This is what the Primary KLey in each table is for. Without it, the DataAdapter has no consistant way of uniquely identifying every record in each tabel in the dataset. For example, when you delete a record out of the DataSet, the DataAdapter executes a SQL DELETE command against that record. That command will look something like this:

        DELETE FROM _tableName_ WHERE _PriKeyColumnName_ = _DataSetPriKeyValue_
        

        This DELETE command is generated by the CommandBuilder object you created. It looks at the SELECT statement and the returned DataSet and uses the information in that set to generate the respective UPDATE, INSERT, and DELETE SQL statements, each with a WHERE clause that identifies which record in the database to execute the statement against. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        S 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Stefan R wrote:

          Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information

          Since your SELECT command is returning all the columns of the Bilder table, this message means that your table doesn't have a Primary Key. DataAdapters will only work with tables that define a Primary Key. This is because changes that are made you the DataSet are written back to the database one record at a time. The DataAdapter looks at each record in the DataSet and determines which SQL Command to execute against that record in order to properly update the database, wheather it be an INSERT, UPDATE, or DELETE command. Now, if the DataAdapter is making changes to the tables in the DataSet one record at a time, how does it know how to tell the database which record to change? This is what the Primary KLey in each table is for. Without it, the DataAdapter has no consistant way of uniquely identifying every record in each tabel in the dataset. For example, when you delete a record out of the DataSet, the DataAdapter executes a SQL DELETE command against that record. That command will look something like this:

          DELETE FROM _tableName_ WHERE _PriKeyColumnName_ = _DataSetPriKeyValue_
          

          This DELETE command is generated by the CommandBuilder object you created. It looks at the SELECT statement and the returned DataSet and uses the information in that set to generate the respective UPDATE, INSERT, and DELETE SQL statements, each with a WHERE clause that identifies which record in the database to execute the statement against. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          S Offline
          S Offline
          Stefan R
          wrote on last edited by
          #4

          I know i can easily delete all records with "delete from biler" and i know the problem is not related to my SQL-statements or db schema. I'm just new to working with the DataSet. Since i want to learn how to work with the dataset i want to avoid using the method above. I want to be able to remove records one by one in the future... The table has a primary key defined in my Access db, however it seems that the DataAdapter doesn't retrieve any meta data about primary keys and constraints from the table( as stated in msdn ). That's probably way the message says that i don't have a primary key. MSDN also states that i should specify which column is the key column somewhere in code and i have read something about MissingSchemaAction.AddWithKey... The problem is that i can't seem to get it right. How do i make the DataAdapter fetch information about the primary key? Or how do i tell the DataAdapter that a certain column in the fetched table is actually a primary key column? This is really bugging me out -- modified at 4:59 Friday 23rd December, 2005

          D 1 Reply Last reply
          0
          • S Stefan R

            I know i can easily delete all records with "delete from biler" and i know the problem is not related to my SQL-statements or db schema. I'm just new to working with the DataSet. Since i want to learn how to work with the dataset i want to avoid using the method above. I want to be able to remove records one by one in the future... The table has a primary key defined in my Access db, however it seems that the DataAdapter doesn't retrieve any meta data about primary keys and constraints from the table( as stated in msdn ). That's probably way the message says that i don't have a primary key. MSDN also states that i should specify which column is the key column somewhere in code and i have read something about MissingSchemaAction.AddWithKey... The problem is that i can't seem to get it right. How do i make the DataAdapter fetch information about the primary key? Or how do i tell the DataAdapter that a certain column in the fetched table is actually a primary key column? This is really bugging me out -- modified at 4:59 Friday 23rd December, 2005

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            There's go to be something wrong with your table then. There's no problem with using a DataAdapter with an Access table. If your table is correct, then you really don't have a need to specify which column is the PK. I've never run into the problem. This code works just fine: [EDIT]Sorry, wrong language for the forum...[/EDIT]

            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\\\TestBlank.mdb"; 
            string sql = "SELECT \* From MyTable"; 
            

            OleDbDataAdapter da = new OleDbDataAdapter(sql, connString);
            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            DataSet ds = new DataSet();
             
            da.Fill(ds, "MyTable");
             
            foreach (DataRow row in ds.Tables("MyTable").Rows)
            {
            Debug.WriteLine("ID: " + row("TestID"));
            row.Delete();
            }
            da.DeleteCommand = cb.GetDeleteCommand();
            da.Update(ds, "MyTable");

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 10:25 Friday 23rd December, 2005

            S 1 Reply Last reply
            0
            • D Dave Kreskowiak

              There's go to be something wrong with your table then. There's no problem with using a DataAdapter with an Access table. If your table is correct, then you really don't have a need to specify which column is the PK. I've never run into the problem. This code works just fine: [EDIT]Sorry, wrong language for the forum...[/EDIT]

              string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\\\TestBlank.mdb"; 
              string sql = "SELECT \* From MyTable"; 
              

              OleDbDataAdapter da = new OleDbDataAdapter(sql, connString);
              OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
              DataSet ds = new DataSet();
               
              da.Fill(ds, "MyTable");
               
              foreach (DataRow row in ds.Tables("MyTable").Rows)
              {
              Debug.WriteLine("ID: " + row("TestID"));
              row.Delete();
              }
              da.DeleteCommand = cb.GetDeleteCommand();
              da.Update(ds, "MyTable");

              RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 10:25 Friday 23rd December, 2005

              S Offline
              S Offline
              Stefan R
              wrote on last edited by
              #6

              I tried changing primary key data type from "PM" to "Text" in the db and now it works just fine. I really did have a primary key though :^), and after reading that thing about "...OleDbDataAdapter not fetching primary key constraints..." on MSDN i really thought i was in trouble. But as always, the problem lies in something small and unsignificant to the eye

              Dave Kreskowiak wrote:

              [EDIT]Sorry, wrong language for the forum...[/EDIT]

              I'm pretty fluent in VB syntax so i don't mind :) Thanks a lot for the help!

              D 1 Reply Last reply
              0
              • S Stefan R

                I tried changing primary key data type from "PM" to "Text" in the db and now it works just fine. I really did have a primary key though :^), and after reading that thing about "...OleDbDataAdapter not fetching primary key constraints..." on MSDN i really thought i was in trouble. But as always, the problem lies in something small and unsignificant to the eye

                Dave Kreskowiak wrote:

                [EDIT]Sorry, wrong language for the forum...[/EDIT]

                I'm pretty fluent in VB syntax so i don't mind :) Thanks a lot for the help!

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Stefan R wrote:

                I'm pretty fluent in VB syntax so i don't mind

                Yeah, I changed the code becauseif you're caught posting VB code in the C# Forum, ... well, let's just say it makes the movie "Desparado" look like a Sunday Brunch. :-D RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                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