我需要能够查看 View 是否已存在于我的 MySQL 数据库中,以及它是否没有创建一个 - 通过 C#。如果可能的话,我真的需要一些帮助和正确的方向。
最佳答案
您可以使用 information_schema 来查询服务器对象(在您的情况下为 View )是否存在。我在 C#
中的示例代码片段如下。
String conn = @"Data Source=myserverName;
Initial Catalog=myCatalogName;Integrated Security=True";
string cmdStr = "select count(*) from
information_schema.views where table_schema = 'mySchemaName'
AND table_name = 'MyViewName'";
using (MySqlConnection conn = new MySqlConnection(conn))
{
MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int count = reader.GetInt32(0);
if (count == 0)
{
MessageBox.Show("View does not exists!");
MySqlCommand command = new MySqlCommand("Create View myView
as select
* from myTable;", conn)
command.ExecuteNonQuery();
}
else if (count == 1)
{
MessageBox.Show("View exists!");
}
conn.Close();
}
}
关于c# - 通过 C# 在 MySQL 中检查和创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23219848/