c#使用SqlSugar连MySql数据库

连接语句:

 
public static SqlSugarClient mysqlDB
{
get => new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = ServerUrl,//必填, 数据库连接字符串
DbType = SqlSugar.DbType.MySql, //必填, 数据库类型
IsAutoCloseConnection = true, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
InitKeyType = InitKeyType.SystemTable //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
});
}

数据库连接字符串:

<add key="connStringmysql" value="Data Source=服务器地址;port=3306; Initial Catalog=库名;uid=账号; pwd=密码"/>

注意:

本地没有安装MySql数据库时需要引用对应的dll文件

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SqlSugar 是一个轻量级的ORM框架,可以方便地操作多种数据库。下面是一个简单的示例,演示如何使用SqlSugar实现多数据库仓储模式。 首先,我们需要定义一个基本的仓储接口,用于定义一些通用的CRUD操作: ```csharp public interface IRepository<T> where T : class, new() { List<T> Query(Expression<Func<T, bool>> whereExpression); T QuerySingle(Expression<Func<T, bool>> whereExpression); void Add(T entity); void Add(List<T> entities); void Update(T entity); void Delete(T entity); void Delete(Expression<Func<T, bool>> whereExpression); } ``` 然后,我们可以为每种数据库定义一个具体的仓储实现类,实现上述接口中的方法。例如,对于MySQL数据库,可以定义如下的仓储实现类: ```csharp public class MySqlRepository<T> : IRepository<T> where T : class, new() { private readonly SqlSugarClient _db; public MySqlRepository(string connectionString) { _db = new SqlSugarClient(new ConnectionConfig { ConnectionString = connectionString, DbType = DbType.MySql, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); } public List<T> Query(Expression<Func<T, bool>> whereExpression) { return _db.Queryable<T>().Where(whereExpression).ToList(); } public T QuerySingle(Expression<Func<T, bool>> whereExpression) { return _db.Queryable<T>().Single(whereExpression); } public void Add(T entity) { _db.Insertable(entity).ExecuteCommand(); } public void Add(List<T> entities) { _db.Insertable(entities).ExecuteCommand(); } public void Update(T entity) { _db.Updateable(entity).ExecuteCommand(); } public void Delete(T entity) { _db.Deleteable(entity).ExecuteCommand(); } public void Delete(Expression<Func<T, bool>> whereExpression) { _db.Deleteable<T>().Where(whereExpression).ExecuteCommand(); } } ``` 类似地,我们还可以定义其他数据库的仓储实现类,例如SQL Server、Oracle等。 最后,我们可以定义一个工厂类,根据需要返回相应的仓储实现类。例如,对于MySQL数据库,可以定义如下的工厂类: ```csharp public class MySqlRepositoryFactory<T> where T : class, new() { private readonly string _connectionString; public MySqlRepositoryFactory(string connectionString) { _connectionString = connectionString; } public IRepository<T> GetRepository() { return new MySqlRepository<T>(_connectionString); } } ``` 这样,我们就可以根据需要使用不同的数据库,而无需修改业务代码。例如,对于MySQL数据库,可以使用如下的代码: ```csharp var factory = new MySqlRepositoryFactory<User>("connectionString"); var repository = factory.GetRepository(); var users = repository.Query(u => u.Name == "张三"); ``` 对于其他数据库也可以使用类似的方式操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值