几何尺寸与公差论坛

 找回密码
 注册
查看: 594|回复: 0

how to know if exist database using sqlSugar.DbMaintenance and c#?

[复制链接]
发表于 2023-2-9 20:28:59 | 显示全部楼层 |阅读模式
You can use the DbMaintenance class in SqlSugar to check if a database exists before attempting to create or manipulate it. The DbMaintenance class provides a method called IsAnyDatabaseExist that you can use for this purpose. Here's an example of how to use it in C#:
using SqlSugar;

// Connect to the database server
var db = new SqlSugarClient("connection string");

// Check if the database exists
var exists = db.DbMaintenance.IsAnyDatabaseExist("database name");

// If the database does not exist, create it
if (!exists)
{
    db.DbMaintenance.CreateDatabase("database name");
}
In this example, "connection string" is the connection string for your database server, and "database name" is the name of the database you want to check. If the database exists, exists will be true, and if it does not exist, exists will be false. You can then use this information to create the database if it does not exist.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 13:09 , Processed in 0.036892 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表