VB.NET - How To Insert Update Delete Data From SQL Server Using Visual Basic.Net
In This VB.NET Tutorial We Will See How To Create Buttons To Insert Data Into SqLServer, Update SqLServer Data, Delete Records From SQLServer Using Visual Basic .NET Programming Language And Microsoft SQL DataBase.
Project Source Code:
Imports System.Data.SqlClient
Public Class VBNET_SQL_Insert_Update_Delete
Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB; Integrated Security = true")
Private Sub BTN_INSERT_Click(sender As Object, e As EventArgs) Handles BTN_INSERT.Click
Dim insertQuery As String = "INSERT INTO Users (Fname,Lname,age) VALUES('" & TextBoxFN.Text & "','" & TextBoxLN.Text & "'," & TextBoxAGE.Text & ")"
ExecuteQuery(insertQuery)
MessageBox.Show("Data Inserted")
End Sub
Public Sub ExecuteQuery(query As String)
Dim command As New SqlCommand(query, connection)
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Sub
Private Sub BTN_UPDATE_Click(sender As Object, e As EventArgs) Handles BTN_UPDATE.Click
Dim updateQuery As String = "Update Users Set Fname = '" & TextBoxFN.Text & "' ,Lname = '" & TextBoxLN.Text & "',age = " & TextBoxAGE.Text & " WHERE Id =" & TextBoxID.Text & ""
ExecuteQuery(updateQuery)
MessageBox.Show("Data Updated")
End Sub
Private Sub BTN_DELETE_Click(sender As Object, e As EventArgs) Handles BTN_DELETE.Click
Dim deleteQuery As String = "delete from Users Where Id = " & TextBoxID.Text
ExecuteQuery(deleteQuery)
MessageBox.Show("User deleted")
End Sub
End Class
///////////////OUTPUT:
Download Projects Source Code