Delete a row and use the datarow. Delete () method. The Delete () method does not perform the delete operation, but only marks the row to be deleted. Use the update method to confirm deletion.
Using System. Data. sqlclient;
Namespace Deleteingdata
{
Public Partial Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
// Delete button event
Btndelete. Click + = New Eventhandler (btndelete_click );
Showdata ();
}
String Sconnection = " Data Source = Scott; initial catalog = northwind; persist Security info = true; user id = sa; Password = sa123 " ;
Dataset dsset = New Dataset ();
Sqldataadapter sdaadapter = Null ;
Sqlcommandbuilder scbbuilder = Null ;
/// <Summary>
/// Show table data
/// </Summary>
Private Void Showdata ()
{
// Establish connection
Sqlconnection scconnection = New Sqlconnection (sconnection );
// CREATE Command
Sqlcommand sccommand = Scconnection. createcommand ();
Sccommand. commandtext = " Select customerid, companyName from MERs " ;
// Create Adapter
Sdaadapter = New Sqldataadapter (sccommand );
// This object is used to generate SQL statements used to update the database. You do not have to create these statements yourself
scbbuilder = New sqlcommandbuilder (sdaadapter);
// get data
sdaadapter. fill (dsset, " MERs " );
dgvview. datasource = dsset. tables [ " MERs " ];
}< br>
VoidBtndelete_click (ObjectSender, eventargs E)
{
Delete ();
}
Private Void Delete ()
{
Datacolumn [] dckeys = New Datacolumn [ 1 ];
Dckeys [ 0 ] = Dsset. Tables [ " MERs " ]. Columns [ " Customerid " ];
// Set the table's primary key
Dsset. Tables [ " MERs " ]. Primarykey = Dckeys;
// Search for rows contained in a primary key
Datarow drfindrow = Dsset. Tables [ " MERs " ]. Rows. Find ( " Zaczi " );
// delete data (this method does not delete data, but only marks the row to be deleted)
drfindrow. delete ();
/// Delete
sdaadapter. update (dsset, " MERs " );
//Re-display data
Dgvview. datasource=Dsset. Tables ["MERs"];
}
}
}