Q:

C#.Net program to drop a table from the MySql database dynamically

belongs to collection: C# Database Connectivity Programs

0

C#.Net program to drop a table from the MySql database dynamically

All Answers

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

Program:

The source code to drop a table from the MySql database dynamically is given below. The given program is compiled and executed successfully.

//C#.NET program to drop a table in MySql database dynamically.

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=Sample_DB";
        MySqlConnection conn = new MySqlConnection(connString);

        conn.Open();

        MySqlCommand cmd = new MySqlCommand("DROP TABLE IF EXISTS employee", conn);
        cmd.ExecuteNonQuery();

        Console.WriteLine("Table employee dropped successfully");
        conn.Close();
    }
}

Output:

Table employee dropped successfully
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 a 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 MySql database using MySqlConnection class and then drop the "employee" table in the database using ExecuteNonQuery() method of MySqlCommand class and print the "Table employee dropped successfully" message 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 insert a record in the MySql dat... >>
<< C#.Net program to create a table in MySql database...