How To Delete Entry From Database

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

//1.

Address of SQL server and Database

string ConnectionString = @"Data Source=(LocalDB)\


MSSQLLocalDB;AttachDbFilename=C:\Users\A R F T\OneDrive\Desktop\MY DATABASE\
MyDb.mdf;Integrated Security=True;Connect Timeout=30";

//2. Estabish Connection

SqlConnection con = new SqlConnection(ConnectionString);

//3. Open Connection

con.Open();

//4. Prepare Query


string NameID = textBox3.Text;

string Query = "DELETE FROM Names WHERE ID=" + NameID;

//5. Execute Query

SqlCommand cmd = new SqlCommand(Query, con);


cmd.ExecuteNonQuery();

//6. Close Connection

con.Close();

MessageBox.Show("Data Has Been DELETED!");


textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";

button2_Click(null, null);

You might also like