Hello!
Here I have a working program that delete the specified row in the database.
It works fine but I want to fully understand it.
In this example the SQLCommandBuild er will automatically create Sqlcommands
for
update, insert and delete.
In this program I specify that I want to delete the row with primary key =
"ZACHI"
So I had hoped that this string would be included in the command
deleteCommand.C ommandText.
When I check this command just before the update command the string ZACHI is
not included at all
in the deleteCommand.
So how can this work if not the string ZACHI is included in the
deleteCommand ?
//Tony
static void Main(string[] args)
{
//Specify SQL Server-specific connection string
using ( SqlConnection thisConnection = new SqlConnection(
@"Server=UHT-DEMO1; Integrated Security=True;" +
"Database=north wind"))
{
//Create DataAdapter object for update and other operations
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"Select CustomerID, CompanyName from Customers",
thisConnection) ;
//Create CommandBuilder object to build SQL commands
SqlCommandBuild er thisBuilder = new
SqlCommandBuild er(thisAdapter) ;
//Create DataSet to contain related data tables, rows, and
columns
DataSet thisDataSet = new DataSet();
//Fill DataSet using query previously defined for
DataAdapter
thisAdapter.Fil l(thisDataSet, "Customers" );
Console.WriteLi ne("# rows before change: {0}",
thisDataSet.Tab les["Customers"].Rows.Count);
//Set up keys object for defining primary key
DataColumn[] keys = new DataColumn[1];
keys[0] =
thisDataSet.Tab les["Customers"].Columns["CustomerID "];
thisDataSet.Tab les["Customers"].PrimaryKey = keys;
DataRow findRow =
thisDataSet.Tab les["Customers"].Rows.Find("ZAC HI");
if (findRow != null)
{
Console.WriteLi ne("ZACHI already in Customers table");
Console.WriteLi ne("Removing ZACHI . . .");
findRow.Delete( );
SqlCommand deleteCommand =
thisBuilder.Get DeleteCommand() ;
Console.WriteLi ne("SQL DELETE comamnd is = \n{0}\n",
deleteCommand.C ommandText);
thisAdapter.Upd ate(thisDataSet , "Customers" );
}
Console.WriteLi ne("# rows after change: {0}",
thisDataSet.Tab les["Customers"].Rows.Count);
}
Console.WriteLi ne("Program finished, press Enter/Return to
continue");
Console.ReadLin e();
}
Here I have a working program that delete the specified row in the database.
It works fine but I want to fully understand it.
In this example the SQLCommandBuild er will automatically create Sqlcommands
for
update, insert and delete.
In this program I specify that I want to delete the row with primary key =
"ZACHI"
So I had hoped that this string would be included in the command
deleteCommand.C ommandText.
When I check this command just before the update command the string ZACHI is
not included at all
in the deleteCommand.
So how can this work if not the string ZACHI is included in the
deleteCommand ?
//Tony
static void Main(string[] args)
{
//Specify SQL Server-specific connection string
using ( SqlConnection thisConnection = new SqlConnection(
@"Server=UHT-DEMO1; Integrated Security=True;" +
"Database=north wind"))
{
//Create DataAdapter object for update and other operations
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"Select CustomerID, CompanyName from Customers",
thisConnection) ;
//Create CommandBuilder object to build SQL commands
SqlCommandBuild er thisBuilder = new
SqlCommandBuild er(thisAdapter) ;
//Create DataSet to contain related data tables, rows, and
columns
DataSet thisDataSet = new DataSet();
//Fill DataSet using query previously defined for
DataAdapter
thisAdapter.Fil l(thisDataSet, "Customers" );
Console.WriteLi ne("# rows before change: {0}",
thisDataSet.Tab les["Customers"].Rows.Count);
//Set up keys object for defining primary key
DataColumn[] keys = new DataColumn[1];
keys[0] =
thisDataSet.Tab les["Customers"].Columns["CustomerID "];
thisDataSet.Tab les["Customers"].PrimaryKey = keys;
DataRow findRow =
thisDataSet.Tab les["Customers"].Rows.Find("ZAC HI");
if (findRow != null)
{
Console.WriteLi ne("ZACHI already in Customers table");
Console.WriteLi ne("Removing ZACHI . . .");
findRow.Delete( );
SqlCommand deleteCommand =
thisBuilder.Get DeleteCommand() ;
Console.WriteLi ne("SQL DELETE comamnd is = \n{0}\n",
deleteCommand.C ommandText);
thisAdapter.Upd ate(thisDataSet , "Customers" );
}
Console.WriteLi ne("# rows after change: {0}",
thisDataSet.Tab les["Customers"].Rows.Count);
}
Console.WriteLi ne("Program finished, press Enter/Return to
continue");
Console.ReadLin e();
}
Comment