How To Delete Values From Database Using SQL In C#…

Now in our early post we learned how to insert the values in the database from coding in C#.

Once you insert a value in the database you should also know how to delete the values from the database.

Deleting is considered to be the easiest thing in SQL, It just takes few lines of code to perform this task but there are some preliminary steps which are involved before performing any SQL query in C#.

Beside the easiness of this query remember the delete query is very power full query and once it is executed then recovering the data is now a piece of cake. Always be fully concentrated while using this query and never write the wrong table name or column name because C# does not show any errors during the compile time,If you have written a wrong query then you have to wait until the code is executed.

There is also a difference between the delete and drop table query for the difference visit our previous blog http://www.farazashraf.wordpress.com. Now lets start the deletion process.

First we will add the namespace of SQL in our code…

So to add namespace first thing you should know is that your data is on which database platform.

Let say you are working on simple Microsoft platform and your data is on access database then you would be using the OLE DB (Object Linking and Embedding, Database) this is an API designed by Microsoft to work on access database.

STEP 1:

So you will start with

Using System;

Using System.Data.OLeDb;

STEP 2:

after adding the namespace now its time for coding in main.

First you  have to create database connection so it will be

OLeDbConnection conn=new OLeDbConnection();

To set the connection string for the connection you need to have a database link. You can either find this while connection database in visual studio or

you can write this connection string.

this is the connection string for access database 2007.

String Connection=(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Files\database1.accdb;”)

For all the connection strings you can visit my previous post on this blog.
www.farazahraf.wordpress.com

STEP3:

To connect OleDb connection with the connection string you can use this line of code.

conn.ConnectionString=connection;

where conn is the object name OLeDb connection class created above you can use any name.

while connection is string name you can also use any name for this word.

STEP4:

Now coming to our main topic of deleting values in the database.
so for this you will have to open connection with database by using this command.

conn.Open();

this will open connection with the database.

now we will write the query for deletion of values in the database.

OLeDBCommand cmd =new OLeDbCommand(“DELETE FROM TABLE1 WHERE ID=001″,conn) ;
cmd.ExecuteNonQuery();

conn.Close();

The (command) class is used here to insert the commands in the data base which will be executed. Query contains a table name which is  “TABLE1″.

“WHERE” clause is used to specify values from which column and which row should be deleted.

Here Column name is “ID” you can specify any column but mostly column with the primary key is used to delete the values from the table.

The id from the values will be deleted is “001” you can use any key to delete.

*Remember if you will not specify the where clause then every row will be deleted from the table so be aware before using the delete query because this is a very power full query and is hard to recover data back.

Remember to always close the connection with con.Close(); after you are done with the deletion or any other command.

I hope you would have liked this.
For any queries regarding database please comment . I will be pleased to answer your queries …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s