public ChangeDatabase ( string databaseName ) : void | ||
databaseName | string | |
return | void |
using MySql.Data.MySqlClient; string connectionString = "server=localhost;user id=root;password=12345;database=exampledb"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); connection.ChangeDatabase("newdb");
using MySql.Data.MySqlClient; string connectionString = "server=localhost;user id=root;password=12345;database=exampledb"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); string databaseName = "newdb"; string query = $"USE {databaseName}"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery();In this example, we again establish a connection to a MySQL database named "exampledb". Instead of using the ChangeDatabase method, we execute a SQL query that changes the database. The package library for MySQL.Data.MySqlClient is called "MySql.Data" and is available on NuGet.