پاک کردن یک رکورد در هنگام اتصال به دیتابیس با کدنویسی
کد:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand();
connection.ConnectionString = @"Data Source=.SQLEXPRESS;" +
"Initial Catalog=University;Integrated Security=SSPI";
command.Connection = connection;
command.CommandText = "DELETE FROM Students WHERE StudentID=@StudentID";
command.Parameters.AddWithValue("@StudentID", studentIdTextBox.Text);
try
{
connection.Open();
int result = command.ExecuteNonQuery();
if (result > 0)
MessageBox.Show("Student was removed!");
else
MessageBox.Show("Can't find student.");
}
catch (SqlException ex)
{
MessageBox.Show("An error has occured.");
}
finally
{
connection.Close();
}
}