VB.NET - How To Insert Data Into SqlServer DataBase Using Visual Basic .Net
In This VB.NET Tutorial We Will See How To Insert Records Into Microsoft SQL DataBase In Visual Basic .NET Programming Language.
Project Source Code:
Imports System.Data.SqlClient
Public Class VBNET_SQL_INSERT
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 command As New SqlCommand("insert into Users(Fname, Lname, age) values('" & TextBoxFN.Text & "','" & TextBoxLN.Text & "'," & TextBoxAGE.Text & ")", connection)
'Or
Dim command As New SqlCommand("insert into Users(Fname, Lname, age) values(@fn,@ln,@age)", connection)
command.Parameters.Add("@fn", SqlDbType.VarChar).Value = TextBoxFN.Text
command.Parameters.Add("@ln", SqlDbType.VarChar).Value = TextBoxLN.Text
command.Parameters.Add("@age", SqlDbType.Int).Value = TextBoxAGE.Text
connection.Open()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("New User Added")
Else
MessageBox.Show("User Not Added")
End If
connection.Close()
End Sub
End Class
///////////////OUTPUT:
Download Projects Source Code
2 comments
commentsCan you share TestDB database?
ReplyThank You sir for this Code very Awesome Your Code
Reply