数据库
首页 > 数据库> > c#检测是否存在数据库(SQL SERVER)

c#检测是否存在数据库(SQL SERVER)

作者:互联网

 private static bool CheckDatabaseExists( string databaseName)
{
{ string sqlCreateDBQuery; bool result = false; try { SqlConnection tmpConn = new SqlConnection(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Example;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;"); sqlCreateDBQuery = string.Format("SELECT database_id from sys.databases WHERE Name = '{0}'", databaseName); using (tmpConn) { using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn)) { tmpConn.Open(); object resultObj = sqlCmd.ExecuteScalar(); int databaseID = 0; if (resultObj != null) { int.TryParse(resultObj.ToString(), out databaseID); } tmpConn.Close(); result = (databaseID > 0); } } } catch (Exception ex) { result = false; } return result; }
}

标签:databaseID,resultObj,False,string,c#,SERVER,result,SQL,tmpConn
来源: https://www.cnblogs.com/jack-jun/p/10537469.html