嗨,大家好我正在尝试编写一些代码来检查MySQL数据库是否存在,然后在控制台中写一行(如果存在或不存在)。
我做了一些谷歌搜索并发现了这段代码,但是我得到了一个语法错误,如果有人知道如何做到这一点,将不胜感激。
Function DoesDBExist(DBConnectionString As String, Logger As String)
Dim exists As Byte = 0
Dim conn As MySqlConnection = New MySqlConnection(DBConnectionString)
Dim cmd As MySqlCommand = New MySqlCommand("SELECT case when exists " & _
"(select 1 from sys.Databases SELECT case when exists " & _
"(select 1 from sys.Databases where Name = @DbName) " & _
"then 1 else 0 end as DbExists", conn)
cmd.Parameters.AddWithValue("@DbName", "MDLUpdate")
conn.Open()
exists = CByte(cmd.ExecuteScalar())
Console.WriteLine(exists)
Console.ReadKey()
conn.Close()
Return CBool(exists)
End Function
请原谅我的标签,我不能100%确定使用哪些标签
答案 0 :(得分:0)
您的代码可能是这样的
Function DoesDBExist(DBConnectionString As String, Logger As String) as Boolean
Dim conn As MySqlConnection = New MySqlConnection(DBConnectionString)
Dim cmd As MySqlCommand = new MySqlCommand("SELECT IF(EXISTS (SELECT SCHEMA_NAME " & _
"FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = @DbName), 'Y','N')", conn)
cmd.Parameters.AddWithValue("@DbName", "MDLUpdate")
conn.Open()
Dim exists As String = cmd.ExecuteScalar().ToString()
Console.WriteLine(exists)
conn.Close()
Return IF(exists = "Y", True, False)
End Function