• We just launched and are currently in beta. Join us as we build and grow the community.

Saving Data Using VB.Net and SQL Server 2018

mickel2000

SEO Keyword Architect
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
65
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, I will teach you how to save the data using vb.net and SQL Server database. This program is developed in Visual Studio 2015 and Microsoft SQL Server Management Studio 2018. It has the ability to save the data in the SQL Server database that you have input in the textbox. Let’s begin.

Creating Database

1. Install the SSMS 2018 on your machine.
2. Open the SSMS 2018 . After that, right click the database, the select “New Database” and name it “dbperson”
2019-10-11.png

3. Do the following query to create a table in the database that you have created.

  1. USE

    [

    dbperson]
  2. GO

  3. /****** Object: Table [dbo].[tblperson] Script Date: 11/10/2019 11:15:11 AM ******/
  4. SET

    ANSI_NULLS ON
  5. GO

  6. SET

    QUOTED_IDENTIFIER ON
  7. GO

  8. CREATE

    TABLE

    [

    dbo]

    .

    [

    tblperson]

    (
  9. [

    PersonID]

    [

    INT

    ]

    IDENTITY

    (

    1

    ,

    1

    )

    NOT

    NULL

    ,
  10. [

    Fname]

    [

    nvarchar]

    (

    50

    )

    NULL

    ,
  11. [

    Lname]

    [

    nvarchar]

    (

    50

    )

    NULL
  12. )

    ON

    [

    PRIMARY

    ]
  13. GO

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for visual basic.
creating_vb_26.png

Step 2

Add a two Textboxes, two Labels, and a Button inside the Form. Then do the Form just like shown below.
2019-10-11_1.png

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access SQL Server

libraries.

  1. Imports System.Data.SqlClient

Step 4

Create a connection between Visual Basic 2015 and SQL Server database. After that, declare and initialize all the classes and variables that are needed.

  1. Dim

    con As

    SqlConnection = New

    SqlConnection("Data Source=.\SQLEXPRESS;Database=dbperson;trusted_connection=true;"

    )
  2. Dim

    cmd As

    SqlCommand
  3. Dim

    sql As

    String
  4. Dim

    result As

    Integer

Step 5

Create a method for saving the data in the database.

  1. Private

    Sub

    saveData(sql As

    String

    )
  2. Try
  3. con.Open

    ()
  4. cmd = New

    SqlCommand
  5. With

    cmd
  6. .Connection = con
  7. .CommandText = sql
  8. Result = .ExecuteNonQuery()
  9. End

    With
  10. If

    Result > 0 Then
  11. MsgBox("Data has been saved in the databse"

    )
  12. End

    If

  13. Catch ex As

    Exception
  14. MsgBox(ex.Message)
  15. Finally
  16. con.Close

    ()
  17. End

    Try
  18. End

    Sub

Step 6

Go back to the design view, double click the button to open the click event

handler of it. After that, add this code inside the “button1_clicked” event to save the data in the database.

  1. Private

    Sub Button1_Click(

    sender As

    Object

    , e As

    EventArgs)

    Handles Button1.

    Click
  2. sql =

    "INSERT INTO tblperson (Fname,Lname) VALUES ('"

    &

    txtFname.

    Text

    &

    "','"

    &

    txtLname.

    Text

    &

    "')"
  3. saveData(

    sql)

  4. txtFname.

    Clear

    (

    )
  5. txtLname.

    Clear

    (

    )
  6. End Sub

Output

2019-10-11_3.png


The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

332,845

332,853

Top