Q:

C#.Net program to get the MySQL version using the \'Select\' statement

belongs to collection: C# Database Connectivity Programs

0

C#.Net program to get the MySQL version using the 'Select' statement

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

//C#.NET program to get MySQL version 
//using "Select" statement.

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=sampledb";
        string MySqlVersion = "";

        MySqlConnection conn = new MySqlConnection(connString);

        conn.Open();

        MySqlCommand cmd = new MySqlCommand("Select VERSION()", conn);

        MySqlVersion = cmd.ExecuteScalar().ToString();

        Console.WriteLine("MySql Version is: " + MySqlVersion);
        conn.Close();
    }
}

Output:

MySql Version is: 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 get the MySql version using ExecuteScalar() method of MySqlCommand class and print the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net program to create a database in MySql dynam... >>
<< C#.Net program to get the MySQL version...