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:
thanks for the post
this very good
was very useful ..... congratulations on your blog
greetings .... thanks
i felt very easy ...tnx...pls keep updating ......
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.
Thank you for your truly helpful blog! I used it and it did work! Thank you so much!
Thank you for your very helpful blog. jejeje It is indeed useful. I used it and it did work! Thank you so much!
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
Post a Comment