VB.NET Insert Data Into Sql Server

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:

vb.net insert data into sqlserver database







Share this

Related Posts

Previous
Next Post »

2 comments

comments
rrs
2 février 2023 à 09:03 delete

Can you share TestDB database?

Reply
avatar
3 mars 2023 à 02:44 delete

Thank You sir for this Code very Awesome Your Code

Reply
avatar