VB.Net program to print the column name of a specified MySql database table

Here, we are going to learn how to print the column name of a specified MySql database table in VB.Net?
Submitted by Nidhi, on February 09, 2021 [Last updated : March 08, 2023]

Prerequisites: Need to install MySQL server and MySQL connector.

VB.Net – Get Column's Name from MySQL Table

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

Program/Source Code:

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

VB.Net code to print the column name of a specified MySql database table

'VB.NET program to print the column names 
'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()

        Console.WriteLine("Employee table columns: ")
        Console.WriteLine(vbTab & reader.GetName(0))
        Console.WriteLine(vbTab & reader.GetName(1))
        Console.WriteLine(vbTab & reader.GetName(2))

        conn.Close()
    End Sub
End Module

Output

Employee table columns:
        eid
        ename
        salary
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 column names using ExecuteReader() and GetName() methods of MySqlDataReader class and then print them on the console screen.

VB.Net Database Connectivity Programs »






Comments and Discussions!

Load comments ↻






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