Skip to content

Commit 4bd3dc3

Browse files
committed
封装Dapper dynamic查询
1 parent d25dba0 commit 4bd3dc3

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

Vue.Net/VOL.Core/Dapper/ISqlDapper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public interface ISqlDapper
2525
/// <returns></returns>
2626
List<T> QueryList<T>(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class;
2727
T QueryFirst<T>(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class;
28+
29+
List<dynamic> QueryDynamicList(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
30+
dynamic QueryDynamicFirst(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
2831
object ExecuteScalar(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
2932

3033
int ExcuteNonQuery(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
@@ -74,7 +77,7 @@ public interface ISqlDapper
7477
int UpdateRange<T>(IEnumerable<T> entities, Expression<Func<T, object>> updateFileds = null, bool beginTransaction = false);
7578

7679
int DelWithKey<T>(params object[] keys);
77-
int DelWithKey<T>(bool beginTransaction = false,params object[] keys);
80+
int DelWithKey<T>(bool beginTransaction = false, params object[] keys);
7881
/// <summary>
7982
/// sqlserver批量写入
8083
/// 使用时DataTable table表字段顺序要和数据库字段顺序一致

Vue.Net/VOL.Core/Dapper/SqlDapper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,21 @@ public List<T> QueryList<T>(string cmd, object param, CommandType? commandType =
107107
}
108108
public T QueryFirst<T>(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class
109109
{
110-
List<T> list = QueryList<T>(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).ToList();
111-
return list.Count == 0 ? null : list[0];
110+
return QueryList<T>(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).FirstOrDefault();
112111
}
112+
113+
public List<dynamic> QueryDynamicList(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
114+
{
115+
return Execute((conn, dbTransaction) =>
116+
{
117+
return conn.Query<dynamic>(cmd, param, dbTransaction, commandType: commandType ?? CommandType.Text).ToList();
118+
}, beginTransaction);
119+
}
120+
public dynamic QueryDynamicFirst(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
121+
{
122+
return QueryList<dynamic>(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).FirstOrDefault();
123+
}
124+
113125
public object ExecuteScalar(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
114126
{
115127
return Execute<object>((conn, dbTransaction) =>

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/Dapper/ISqlDapper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public interface ISqlDapper
2525
/// <returns></returns>
2626
List<T> QueryList<T>(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class;
2727
T QueryFirst<T>(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class;
28+
29+
List<dynamic> QueryDynamicList(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
30+
dynamic QueryDynamicFirst(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
2831
object ExecuteScalar(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
2932

3033
int ExcuteNonQuery(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false);
@@ -74,7 +77,7 @@ public interface ISqlDapper
7477
int UpdateRange<T>(IEnumerable<T> entities, Expression<Func<T, object>> updateFileds = null, bool beginTransaction = false);
7578

7679
int DelWithKey<T>(params object[] keys);
77-
int DelWithKey<T>(bool beginTransaction = false,params object[] keys);
80+
int DelWithKey<T>(bool beginTransaction = false, params object[] keys);
7881
/// <summary>
7982
/// sqlserver批量写入
8083
/// 使用时DataTable table表字段顺序要和数据库字段顺序一致

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/Dapper/SqlDapper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,21 @@ public List<T> QueryList<T>(string cmd, object param, CommandType? commandType =
107107
}
108108
public T QueryFirst<T>(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false) where T : class
109109
{
110-
List<T> list = QueryList<T>(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).ToList();
111-
return list.Count == 0 ? null : list[0];
110+
return QueryList<T>(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).FirstOrDefault();
112111
}
112+
113+
public List<dynamic> QueryDynamicList(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
114+
{
115+
return Execute((conn, dbTransaction) =>
116+
{
117+
return conn.Query<dynamic>(cmd, param, dbTransaction, commandType: commandType ?? CommandType.Text).ToList();
118+
}, beginTransaction);
119+
}
120+
public dynamic QueryDynamicFirst(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
121+
{
122+
return QueryList<dynamic>(cmd, param, commandType: commandType ?? CommandType.Text, beginTransaction: beginTransaction).FirstOrDefault();
123+
}
124+
113125
public object ExecuteScalar(string cmd, object param, CommandType? commandType = null, bool beginTransaction = false)
114126
{
115127
return Execute<object>((conn, dbTransaction) =>

0 commit comments

Comments
 (0)