如何检查mysql服务器中是否存在数据库? [英] How to check if database exists in mysql server?
本文介绍了如何检查mysql服务器中是否存在数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在使用带有MySQL数据库的WPF应用程序。我确实创建了backup.sql文件。然后,在创建.exe文件之后,如何检查db是否存在然后跳过下面的代码?
请帮帮我...
我尝试了什么:
我用过这段代码,
Hello everyone,
I am using WPF apps with MySQL database. I did create backup.sql file. Then, after create .exe file, how can I check if db exist then skip below code?
Please help me...
What I have tried:
I did used this code,
private void Application_Startup(object sender, StartupEventArgs e)
{
RestoreBehavior();
}
在这里,我如何检查mydb是否存在然后跳过代码?
Here,How can I check here if mydb exist then skip the code?
private void RestoreBehavior()
{
string constring = "server=localhost;user=root;pwd=123456;";
string file = @"C:\Program Files (x86)\xxxx\xxxx\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ImportInfo.TargetDatabase = "mydb";/*Here,How can I check here if mydb exist then skip the code?*/
mb.ImportInfo.DatabaseDefaultCharSet = "utf8";
mb.ImportFromFile(file);
}
}
}
}
推荐答案
尝试:
Try:
use information_schema;
SELECT COUNT(*) FROM schemata WHERE SCHEMA_NAME="<db name here>";
public bool DBExists(string conn, string dbName)
{
bool functionReturnValue = false;
using (MySqlConnection dbconn = new MySqlConnection(conn)) {
using (MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM information_schema.schemata WHERE SCHEMA_NAME=@dbName", conn)) {
functionReturnValue = false;
cmd.Parameters.AddWithValue("@dbName", dbName);
dbconn.Open();
if (cmd.ExecuteNonQuery() != 0) {
functionReturnValue = true;
}
dbconn.Close();
}
}
return functionReturnValue;
}
这篇关于如何检查mysql服务器中是否存在数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文