private static bool CheckDatabaseExists( string databaseName){{string sqlCreateDBQuery;bool result = false;try{SqlConnection tmpConn = new SqlConnection(@Data Source=(localdb)\ProjectsV13;Initial Cat...
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; }
}
沃梦达教程
本文标题为:c#检测是否存在数据库(SQL SERVER)
基础教程推荐
猜你喜欢
- C#中Socket与Unity相结合示例代码 2022-11-15
- c#中LINQ的基本用法(一) 2023-05-31
- Unity3D绘制地形的实现方法 2023-01-16
- unity 实现摄像机绕某点旋转一周 2023-04-10
- c# 实现观察者模式 2023-03-28
- WPF程序将控件所呈现的内容保存成图像 2023-06-15
- C#调用C++动态库接口函数和回调函数方法 2023-05-26
- C# 使用CancellationTokenSource取消多线程 2023-04-27
- Unity登录注册时限制发送验证码次数功能的解决方法 2023-02-06
- C# 显示、隐藏窗口对应的任务栏 2023-05-06