Connect to SQL database with VB.net

Learn VB.Net Programming in simple and easy steps.

VB.net tutorial for beginner.




How to connect SQL database using VB.net programming.

Establish a successful connection to a Microsoft SQL Server Database using VB.net







source code:

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Form1
   
    Dim con As New SqlConnection
    Dim sql As String
    Dim ds As New DataSet
    Dim da As SqlDataAdapter

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        
        con.ConnectionString = ("server=POS16-PC\SQLEXPRESS;database=aa;user=sa;pwd=100;")

        con.Open()

        MsgBox("Database is now open")

        sql = "select * from student"
       
        da = New SqlDataAdapter(sql, con)

        da.Fill(ds, "test")

        DataGridView1.DataSource = ds.Tables("test")

        con.Close()

        MsgBox("Database is now Closed")

    End Sub

End Class