我正在使用下面的代码来查找数据库是否存在但ExecuteNonQuery
总是返回-1。
我已经看到了master.sys.databases
视图,它有数据库POS
SqlConnection tmpConn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
sqlCheckDBQuery = "SELECT * FROM master.sys.databases where name = \'aspnetdb\'";
using (tmpConn)
{
try
{
tmpConn.Open();
tmpConn.ChangeDatabase("master");
}
catch (Exception)
{
MessageBox.Show("SQLServer Express Database is either not installed or not running!", "Database Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
using (SqlCommand sqlCmd = new SqlCommand(sqlCheckDBQuery, tmpConn))
{
int exists = sqlCmd.ExecuteNonQuery();
if (exists <= 0)
databaseExists = false;
else
databaseExists = true;
}
}