diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index f5d2d1db..29d4f3d4 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1369,6 +1369,16 @@ 参数 + + + 原生sql语法条件,Where("id = @id", new { id = 1 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + true 时生效 + sql语法条件 + 参数 + + 传入实体,将主键作为条件 @@ -3089,177 +3099,6 @@ - - - 测试数据库是否连接正确,本方法执行如下命令: - MySql/SqlServer/PostgreSQL/达梦/人大金仓/神通: SELECT 1 - Oracle: SELECT 1 FROM dual - - 命令超时设置(秒) - - true: 成功, false: 失败 - - - - 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】 - - - - - - - - - - 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 查询 - - - - - - - - - 查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 查询 - - - - - - - - - 查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 查询 - - - - - - - - - 查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 在【主库】执行 - - - - - - - - - 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 在【主库】执行 - - - - - - - - - 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 }) - - - - - - - - - - - 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - - - 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 }) - - - - - - - - - - - - 执行SQL返回对象集合,Query<User, Address>("select * from user where age > @age; select * from address", new { age = 25 }) - 提示:parms 参数还可以传 Dictionary<string, object> - - - - - - - - 可自定义解析表达式 @@ -4088,12 +3927,6 @@ 超时 - - - 获取资源 - - - 使用完毕后,归还资源 @@ -4164,12 +3997,6 @@ 资源对象 - - - 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 - - 资源对象 - 归还对象给对象池的时候触发 diff --git a/FreeSql/Interface/Curd/IDelete.cs b/FreeSql/Interface/Curd/IDelete.cs index 7a034789..54ed617a 100644 --- a/FreeSql/Interface/Curd/IDelete.cs +++ b/FreeSql/Interface/Curd/IDelete.cs @@ -53,6 +53,15 @@ namespace FreeSql /// IDelete Where(string sql, object parms = null); /// + /// 原生sql语法条件,Where("id = @id", new { id = 1 }) + /// 提示:parms 参数还可以传 Dictionary<string, object> + /// + /// true 时生效 + /// sql语法条件 + /// 参数 + /// + IDelete WhereIf(bool condition, string sql, object parms = null); + /// /// 传入实体,将主键作为条件 /// /// 实体 diff --git a/FreeSql/Internal/CommonProvider/DeleteProvider.cs b/FreeSql/Internal/CommonProvider/DeleteProvider.cs index e6530fd5..448ff585 100644 --- a/FreeSql/Internal/CommonProvider/DeleteProvider.cs +++ b/FreeSql/Internal/CommonProvider/DeleteProvider.cs @@ -98,9 +98,10 @@ namespace FreeSql.Internal.CommonProvider if (condition == false || exp == null) return this; return this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp?.Body, null, _params)); } - public IDelete Where(string sql, object parms = null) + public IDelete Where(string sql, object parms = null) => WhereIf(true, sql, parms); + public IDelete WhereIf(bool condition, string sql, object parms = null) { - if (string.IsNullOrEmpty(sql)) return this; + if (condition == false || string.IsNullOrEmpty(sql)) return this; if (++_whereTimes > 1) _where.Append(" AND "); _where.Append('(').Append(sql).Append(')'); if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject(sql, parms));