×

VB.Net Programs

VB.Net program to get the records of a specified MySql database table

By Nidhi Last Updated : November 15, 2024

Prerequisites: Need to install MySQL server and MySQL connector.

VB.Net – Get All Records from MySQL Table

In this program, here we will connect to the MySQL database and get "employee" records of the employee table then print them on the console screen.

Program/Source Code:

The source code to get the records of a specified MySql database table is given below. The given program is compiled and executed successfully.

VB.Net code to get the records of a specified MySql database table

'VB.NET program to get the records of a 
'specified MySql database table

Imports MySql.Data.MySqlClient

Module Module1
    Sub Main()
        Dim connString As String

        'Connection String to connect with MySQL database.
        connString = "server=localhost;userid=root;password=root;database=sampledb"

        Dim conn As New MySqlConnection(connString)

        conn.Open()
        Dim query = "SELECT * FROM employee"

        Dim cmd As New MySqlCommand(query, conn)
        Dim reader As MySqlDataReader

        reader = cmd.ExecuteReader()

        While (reader.Read())
            Console.WriteLine(reader.GetInt32(0) & " " & reader.GetString(1) & " " & reader.GetUInt32(2))
        End While

        conn.Close()
    End Sub
End Module

Output

101 Amit Kumar 43200
102 Vikas 20000
Press any key to continue . . .

Explanation

In the above program, we imported a namespace MySql.Data.MySqlClient to establish the connection with the MySql database. Then we created a module Module1 that contains a Main() function.

The Main() function is the entry point for the program. In the Main() function, we created a connection string variable ConnString that contains the database connectivity credentials. After that, we established the connection to the MySql database using MySqlConnection class and then get the employee records using MySqlDataReader class. Here, we used Read() method to record one by one and we used GetInt32(), GetString() methods to read column values and print records on the console screen.

VB.Net Database Connectivity Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.