Free Information Technology Magazines and eBooks

Thursday, June 11, 2009

VB.NET: How to DELETE record on SQL Database

This topic is part of VB.NET SQL Database Basics Tutorial series. On this article, you will learn how to incorporate SQL DELETE statements on your VB.NET application. An SQL DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed. For this article, we will focus particularly on updating records in Microsoft SQL Server.



Note: I assumed that you already installed your SQL Server and Visual Studio .NET.

Here is the step-by-step procedure to UPDATE record on SQL server:
1. Create your VB.NET project.
2. Include the following namespaces.


Imports System.Data
Imports System.Data.SqlClient


The System.Data namespace provides access to classes that represent the ADO.NET architecture while the System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server.

3. Declare and instantiate your SQLConnection object and Command object as shown below


Dim con As New SqlConnection
Dim cmd As New SqlCommand


4. Pass the SQL connection string to ConnectionString property of your SqlConnection object.

con.ConnectionString = "Data Source=atisource;Initial
Catalog=BillingSys;Persist Security Info=True;User ID=sa;Password=12345678"


5. Invoke the Open Method to connect to SQL Server.


con.Open()


6. Set the connection of the command object.


cmd.Connection = con


7. Pass the DELETE SQL statement to the command object commandtext as shown below


cmd.CommandText = "DELETE FROM table_name [WHERE condition]"
'For Example:
'DELETE FROM customer WHERE firstname='Fryan0911'


8. Use the ExecuteNonQuery() method to run the DELETE SQL statement.


cmd.ExecuteNonQuery()


The full sample source code of deleting records on SQL Database:


Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=atisource;Initial Catalog=BillingSys;Persist Security Info=True;User ID=sa;Password=12345678"
con.Open()
cmd.Connection = con
cmd.CommandText = "DELETE FROM table_name WHERE field1='Test'"
cmd.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
con.Close()
End Try






6 comments:

Daniel said...

thanks for the post
this very good
was very useful ..... congratulations on your blog
greetings .... thanks

kannadigaru naavu said...

i felt very easy ...tnx...pls keep updating ......

tpi said...

Your code is very easy to understand but can you explain how you whold insert, update and delete a record making use of text boxes and stored procedures in VB.NET. Your code seems to help much on connecting to sql server, if you insert, update and delete without using vb controls like text boxes and sql stored procedures then its like you testing some stored produres whilst you are in sql server, not vb.net.

So can you please assist.

Thanks.

18kurama said...

Thank you for your truly helpful blog! I used it and it did work! Thank you so much!

18kurama said...

Thank you for your very helpful blog. jejeje It is indeed useful. I used it and it did work! Thank you so much!

Anonymous said...

ive spent a solid day trying to learn this stuff for sql for my internship. this was a exactly what I needed. Def going to stay on my bookmarks. Thanks