G
Guest
Hi, I'm studying C# now, but I'm used to work with DB in other programming
languages.
I'm reading about DataAdapters and inserting, updating, and deletting data
from in a table. I got the example below:
provate static void InsertNewCar(SqlDataAdapter dAdapter)
{
//Get info about new car to insert
....
string sql = string.Format("Insert Into Invetory"+
"CarId, Make, Color, PetName) Values"+
"('{0}', '{1}', '{2}', '{3}')",
newCarId, newCarMake, newCarColor, newCarPetName);
dAdapter.InsertCommand = new SqlCommand(sql);
dAdapter.InsertCommand.Connection = cnObj;
DataRow newCar = dsCarInventory.Tables["Inventory"].NewRow();
newCar["CarId"] = newCarId;
....
newCar["PetName"] = newCarPetName;
dsCarInventory.Tables["Inventory"].Rows.Add(newCar);
dAdapter.Update(dsCarInventory.Tables["Inventory"]);
}
What I don't understand here is why it seems to be duplicated the insert to
the DB. First an SQL statement is created, this statment by itself is able to
insert the new Car to the DB. After that a new row is inserted manually
directly to the table.
Please, can anyone explain me this example.
Thanks a lot.
languages.
I'm reading about DataAdapters and inserting, updating, and deletting data
from in a table. I got the example below:
provate static void InsertNewCar(SqlDataAdapter dAdapter)
{
//Get info about new car to insert
....
string sql = string.Format("Insert Into Invetory"+
"CarId, Make, Color, PetName) Values"+
"('{0}', '{1}', '{2}', '{3}')",
newCarId, newCarMake, newCarColor, newCarPetName);
dAdapter.InsertCommand = new SqlCommand(sql);
dAdapter.InsertCommand.Connection = cnObj;
DataRow newCar = dsCarInventory.Tables["Inventory"].NewRow();
newCar["CarId"] = newCarId;
....
newCar["PetName"] = newCarPetName;
dsCarInventory.Tables["Inventory"].Rows.Add(newCar);
dAdapter.Update(dsCarInventory.Tables["Inventory"]);
}
What I don't understand here is why it seems to be duplicated the insert to
the DB. First an SQL statement is created, this statment by itself is able to
insert the new Car to the DB. After that a new row is inserted manually
directly to the table.
Please, can anyone explain me this example.
Thanks a lot.