C# program to get the MySQL version

Here, we will learn how to get the MySQL version using C# program? By Nidhi Last updated : April 04, 2023

Problem Statement

In this program, here we will connect to the MySQL database and print the version of MySQL on the console screen.

C# code to get the MySQL version

The source code to get the MySQL version is given below. The given program is compiled and executed successfully.

// C# program to get MySQL version.

using MySql.Data.MySqlClient;
using System;

class Program {
  static void Main(string[] args) {
    //Connection String to connect with MySQL database.
    string connString = "server=localhost;userid=root;password=root;database=mydb";
    MySqlConnection conn = new MySqlConnection(connString);

    conn.Open();
    Console.WriteLine("MySQL version : " + conn.ServerVersion);
    conn.Close();
  }
}

Output

MySQL version : 8.0.22
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 class Program that contains the Main() method.

The Main() method is the entry point for the program. In the Main() Method, we created a connection string variable ConnString that contains the database connectivity credentials. After that, we established the connection to the database using MySqlConnection class and then print the MySql version using the ServerVersion property on the console screen.

C# Database Programs »




Comments and Discussions!

Load comments ↻





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