Delete command with DataSet and DataAdapter
-
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? -
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?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
-
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?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
-
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
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 -
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, 2005There'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
-
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
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!
-
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!
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