This is simple task which can be accomplished with below code. Below code is useful to Delete data (contact) from SQL Server database using C#.net .
public void DeleteContact_OnClick(object sender, EventArgs e)
{
string myDeleteCommand = @"DELETE Contact where ID=@ID";
SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog = DataBaseName; Integrated Security=SSPI");
SqlCommand cmd = new SqlCommand(myDeleteCommand , connection );
cmd.Parameters.AddWithValue("@ID", Convert.ToInt32( tbID.Text));
try
{
connection .Open();
cmd.ExecuteNonQuery();
}
catch(Exception e)
{ }
finally
{
connection .Close();
}
}
Connection string's options :
public void DeleteContact_OnClick(object sender, EventArgs e)
{
string myDeleteCommand = @"DELETE Contact where ID=@ID";
SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog = DataBaseName; Integrated Security=SSPI");
SqlCommand cmd = new SqlCommand(myDeleteCommand , connection );
cmd.Parameters.AddWithValue("@ID", Convert.ToInt32( tbID.Text));
try
{
connection .Open();
cmd.ExecuteNonQuery();
}
catch(Exception e)
{ }
finally
{
connection .Close();
}
}
Connection string's options :
- Integrated Security=SSPI; – This means we want to connect using Windows authentication.
- Initial Catalog=DataBaseName; – This means the database we want to first connect to is named “DataBaseName”.
- Data Source=(local); – This means that we want to connect to the SQL Server instance located on the local machine.
No comments:
Post a Comment