diff --git a/Dapper.Lite/Dapper.Lite/Dapper.Lite.csproj b/Dapper.Lite/Dapper.Lite/Dapper.Lite.csproj index b85781f..d922858 100644 --- a/Dapper.Lite/Dapper.Lite/Dapper.Lite.csproj +++ b/Dapper.Lite/Dapper.Lite/Dapper.Lite.csproj @@ -6,9 +6,9 @@ disable True Dapper.Lite - 2.0.7 - 2.0.7 - 2.0.7 + 2.0.8 + 2.0.8 + 2.0.8 Dapper.Lite https://github.com/0611163/Dapper.Lite MIT @@ -20,7 +20,10 @@ 更新内容: - 1. 移除接口的where T : new()限制,以支持QueryList<string> + 1. 新增:IDbSession接口增加SetCommandType方法,以支持调用存储过程 + 2. 优化:删除重复的OnExecuting调用 + 3. 新增:IDbSession接口新增查询并返回DataTable的方法 + 4. 新增:IDbSession接口增加设置Dapper参数的方法SetCommandTimeout和SetBuffered diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSession.cs b/Dapper.Lite/Dapper.Lite/Session/DbSession.cs index c023afc..ce548df 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSession.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSession.cs @@ -86,6 +86,12 @@ public partial class DbSession : IDbSession /// public DbConnection Conn => _tran?.Connection; + private CommandType _commandType = CommandType.Text; + + private int? _commandTimeout = null; // Dapper参数 + + private bool _buffered = true; // Dapper参数 + #endregion #region 静态构造函数 @@ -254,5 +260,36 @@ public async Task GetOpenedConnectionAsync(DbTransaction tran = nu } #endregion + #region 设置CommandType + /// + /// 设置CommandType + /// + public IDbSession SetCommandType(CommandType commandType) + { + _commandType = commandType; + return this; + } + #endregion + + #region Dapper参数设置 + /// + /// 设置Dapper参数commandTimeout + /// + public IDbSession SetCommandTimeout(int? commandTimeout) + { + _commandTimeout = commandTimeout; + return this; + } + + /// + /// 设置Dapper参数buffered + /// + public IDbSession SetBuffered(bool buffered) + { + _buffered = buffered; + return this; + } + #endregion + } } diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSessionDelete.cs b/Dapper.Lite/Dapper.Lite/Session/DbSessionDelete.cs index 9f3917d..b82f07f 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSessionDelete.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSessionDelete.cs @@ -47,8 +47,6 @@ public int DeleteById(string id) Tuple delTmpl = _provider.CreateDeleteSqlTempldate(); sbSql.Append(string.Format(delTmpl.Item1 + " {0} " + delTmpl.Item2 + " {1}={2}", GetTableName(_provider, type), idNameWithQuote, _provider.GetParameterName(idName, idType))); - OnExecuting?.Invoke(sbSql.ToString(), cmdParms); - return Execute(sbSql.ToString(), cmdParms); } #endregion @@ -90,8 +88,6 @@ public async Task DeleteByIdAsync(string id) Tuple delTmpl = _provider.CreateDeleteSqlTempldate(); sbSql.Append(string.Format(delTmpl.Item1 + " {0} " + delTmpl.Item2 + " {1}={2}", GetTableName(_provider, type), idNameWithQuote, _provider.GetParameterName(idName, idType))); - OnExecuting?.Invoke(sbSql.ToString(), cmdParms); - return await ExecuteAsync(sbSql.ToString(), cmdParms); } #endregion @@ -122,8 +118,6 @@ public int BatchDeleteByIds(string ids) sbSql.Remove(sbSql.Length - 1, 1); sbSql.Append(")"); - OnExecuting?.Invoke(sbSql.ToString(), cmdParms); - return Execute(sbSql.ToString(), cmdParms); } #endregion @@ -153,8 +147,6 @@ public async Task BatchDeleteByIdsAsync(string ids) sbSql.Remove(sbSql.Length - 1, 1); sbSql.Append(")"); - OnExecuting?.Invoke(sbSql.ToString(), cmdParms); - return await ExecuteAsync(sbSql.ToString(), cmdParms); } #endregion @@ -200,8 +192,6 @@ public int DeleteByCondition(Type type, string condition) Tuple delTmpl = _provider.CreateDeleteSqlTempldate(); sbSql.Append(string.Format(delTmpl.Item1 + " {0} " + delTmpl.Item2 + " {1}", GetTableName(_provider, type), condition)); - OnExecuting?.Invoke(sbSql.ToString(), null); - return Execute(sbSql.ToString()); } #endregion @@ -219,8 +209,6 @@ public async Task DeleteByConditionAsync(Type type, string condition) Tuple delTmpl = _provider.CreateDeleteSqlTempldate(); sbSql.Append(string.Format(delTmpl.Item1 + " {0} " + delTmpl.Item2 + " {1}", GetTableName(_provider, type), condition)); - OnExecuting?.Invoke(sbSql.ToString(), null); - return await ExecuteAsync(sbSql.ToString()); } #endregion @@ -265,8 +253,6 @@ public int DeleteByCondition(Type type, string condition, DbParameter[] cmdParms Tuple delTmpl = _provider.CreateDeleteSqlTempldate(); sbSql.Append(string.Format(delTmpl.Item1 + " {0} " + delTmpl.Item2 + " {1}", GetTableName(_provider, type), condition)); - OnExecuting?.Invoke(sbSql.ToString(), cmdParms); - return Execute(sbSql.ToString(), cmdParms); } #endregion @@ -284,8 +270,6 @@ public async Task DeleteByConditionAsync(Type type, string condition, DbPar Tuple delTmpl = _provider.CreateDeleteSqlTempldate(); sbSql.Append(string.Format(delTmpl.Item1 + " {0} " + delTmpl.Item2 + " {1}", GetTableName(_provider, type), condition)); - OnExecuting?.Invoke(sbSql.ToString(), cmdParms); - return await ExecuteAsync(sbSql.ToString(), cmdParms); } #endregion diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSessionExecuteCommand.cs b/Dapper.Lite/Dapper.Lite/Session/DbSessionExecuteCommand.cs index 12f63a7..4fda857 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSessionExecuteCommand.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSessionExecuteCommand.cs @@ -28,7 +28,6 @@ public partial class DbSession : IDbSession public bool Exists(string sqlString) { SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, null); object obj = ExecuteScalar(sqlString); @@ -50,7 +49,6 @@ public bool Exists(string sqlString) public async Task ExistsAsync(string sqlString) { SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, null); object obj = await ExecuteScalarAsync(sqlString); @@ -74,7 +72,6 @@ public async Task ExistsAsync(string sqlString) public T QuerySingle(string sqlString) { SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, null); object obj = ExecuteScalar(sqlString); @@ -97,7 +94,6 @@ public T QuerySingle(string sqlString) public object QuerySingle(string sqlString) { SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, null); object obj = ExecuteScalar(sqlString); @@ -120,7 +116,6 @@ public object QuerySingle(string sqlString) public async Task QuerySingleAsync(string sqlString) { SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, null); object obj = await ExecuteScalarAsync(sqlString); @@ -143,7 +138,6 @@ public async Task QuerySingleAsync(string sqlString) public async Task QuerySingleAsync(string sqlString) { SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, null); object obj = await ExecuteScalarAsync(sqlString); @@ -222,9 +216,6 @@ public Task QueryCountAsync(string sql) /// public bool Exists(string sqlString, DbParameter[] cmdParms) { - SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, cmdParms); - object obj = ExecuteScalar(sqlString, cmdParms); if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value)) @@ -244,9 +235,6 @@ public bool Exists(string sqlString, DbParameter[] cmdParms) /// public async Task ExistsAsync(string sqlString, DbParameter[] cmdParms) { - SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, cmdParms); - object obj = ExecuteScalarAsync(sqlString, cmdParms); if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value)) @@ -269,9 +257,6 @@ public async Task ExistsAsync(string sqlString, DbParameter[] cmdParms) /// 参数 public T QuerySingle(string sqlString, DbParameter[] cmdParms) { - SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, cmdParms); - object obj = ExecuteScalar(sqlString, cmdParms); if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value)) @@ -293,9 +278,6 @@ public T QuerySingle(string sqlString, DbParameter[] cmdParms) /// 参数 public object QuerySingle(string sqlString, DbParameter[] cmdParms) { - SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, cmdParms); - object obj = ExecuteScalar(sqlString, cmdParms); if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value)) @@ -317,9 +299,6 @@ public object QuerySingle(string sqlString, DbParameter[] cmdParms) /// 参数 public async Task QuerySingleAsync(string sqlString, DbParameter[] cmdParms) { - SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, cmdParms); - object obj = await ExecuteScalarAsync(sqlString, cmdParms); if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value)) @@ -341,9 +320,6 @@ public async Task QuerySingleAsync(string sqlString, DbParameter[] cmdParm /// 参数 public async Task QuerySingleAsync(string sqlString, DbParameter[] cmdParms) { - SqlFilter(ref sqlString); - OnExecuting?.Invoke(sqlString, cmdParms); - object obj = await ExecuteScalarAsync(sqlString, cmdParms); if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value)) @@ -537,11 +513,13 @@ public Task QueryCountAsync(ISqlString sql, int pageSize) #region ExecuteScalar internal object ExecuteScalar(string sqlString, DbParameter[] cmdParms = null) { + OnExecuting?.Invoke(sqlString, cmdParms); + var conn = GetConnection(_tran); try { - return conn.ExecuteScalar(sqlString, ToDynamicParameters(cmdParms), _tran); + return conn.ExecuteScalar(sqlString, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType); } finally { @@ -556,11 +534,13 @@ internal object ExecuteScalar(string sqlString, DbParameter[] cmdParms = null) #region ExecuteScalarAsync internal async Task ExecuteScalarAsync(string sqlString, DbParameter[] cmdParms = null) { + OnExecuting?.Invoke(sqlString, cmdParms); + var conn = GetConnection(_tran); try { - return await conn.ExecuteScalarAsync(sqlString, ToDynamicParameters(cmdParms), _tran); + return await conn.ExecuteScalarAsync(sqlString, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType); } finally { @@ -575,11 +555,13 @@ internal async Task ExecuteScalarAsync(string sqlString, DbParameter[] c #region Execute internal int Execute(string sqlString, DbParameter[] cmdParms = null) { + OnExecuting?.Invoke(sqlString, cmdParms); + var conn = GetConnection(_tran); try { - return conn.Execute(sqlString, ToDynamicParameters(cmdParms), _tran); + return conn.Execute(sqlString, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType); } finally { @@ -594,11 +576,124 @@ internal int Execute(string sqlString, DbParameter[] cmdParms = null) #region ExecuteAsync internal async Task ExecuteAsync(string sqlString, DbParameter[] cmdParms = null) { + OnExecuting?.Invoke(sqlString, cmdParms); + + var conn = GetConnection(_tran); + + try + { + return await conn.ExecuteAsync(sqlString, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType); + } + finally + { + if (_tran == null) + { + if (conn.State != ConnectionState.Closed) conn.Close(); + } + } + } + #endregion + + + #region 查询并返回DataTable + /// + /// 执行查询语句,返回DataTable + /// + public DataTable QueryDataTable(string sqlString) + { + SqlFilter(ref sqlString); + OnExecuting?.Invoke(sqlString, null); + + var conn = GetConnection(_tran); + + try + { + var reader = conn.ExecuteReader(sqlString, null, _tran, _commandTimeout, _commandType); + DataTable dt = new DataTable(); + dt.Load(reader); + return dt; + } + finally + { + if (_tran == null) + { + if (conn.State != ConnectionState.Closed) conn.Close(); + } + } + } + + /// + /// 执行查询语句,返回DataTable + /// + public async Task QueryDataTableAsync(string sqlString) + { + SqlFilter(ref sqlString); + OnExecuting?.Invoke(sqlString, null); + + var conn = GetConnection(_tran); + + try + { + var reader = await conn.ExecuteReaderAsync(sqlString, null, _tran, _commandTimeout, _commandType); + DataTable dt = new DataTable(); + dt.Load(reader); + return dt; + } + finally + { + if (_tran == null) + { + if (conn.State != ConnectionState.Closed) conn.Close(); + } + } + } + + /// + /// 执行查询语句,返回DataTable + /// + /// 查询语句 + /// 参数 + /// IDataReader + public DataTable QueryDataTable(string sqlString, DbParameter[] cmdParms) + { + OnExecuting?.Invoke(sqlString, cmdParms); + + var conn = GetConnection(_tran); + + try + { + var reader = conn.ExecuteReader(sqlString, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType); + DataTable dt = new DataTable(); + dt.Load(reader); + return dt; + } + finally + { + if (_tran == null) + { + if (conn.State != ConnectionState.Closed) conn.Close(); + } + } + } + + /// + /// 执行查询语句,返回DataTable + /// + /// 查询语句 + /// 参数 + /// IDataReader + public async Task QueryDataTableAsync(string sqlString, DbParameter[] cmdParms) + { + OnExecuting?.Invoke(sqlString, cmdParms); + var conn = GetConnection(_tran); try { - return await conn.ExecuteAsync(sqlString, ToDynamicParameters(cmdParms), _tran); + var reader = await conn.ExecuteReaderAsync(sqlString, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType); + DataTable dt = new DataTable(); + dt.Load(reader); + return dt; } finally { diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSessionFind.cs b/Dapper.Lite/Dapper.Lite/Session/DbSessionFind.cs index d70ed3b..f41b6fd 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSessionFind.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSessionFind.cs @@ -212,7 +212,7 @@ private object Find(Type type, string sql, DbParameter[] parameters) try { - return conn.QueryFirstOrDefault(type, sql, ToDynamicParameters(parameters), _tran); + return conn.QueryFirstOrDefault(type, sql, ToDynamicParameters(parameters), _tran, _commandTimeout, _commandType); } finally { @@ -237,7 +237,7 @@ private async Task FindAsync(Type type, string sql, DbParameter[] parame try { - return await conn.QueryFirstOrDefaultAsync(type, sql, ToDynamicParameters(parameters), _tran); + return await conn.QueryFirstOrDefaultAsync(type, sql, ToDynamicParameters(parameters), _tran, _commandTimeout, _commandType); } finally { diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSessionFindList.cs b/Dapper.Lite/Dapper.Lite/Session/DbSessionFindList.cs index 3f1ebe8..701ef31 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSessionFindList.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSessionFindList.cs @@ -26,7 +26,7 @@ public List QueryList(string sql) try { - return conn.Query(sql, null, _tran).ToList(); + return conn.Query(sql, null, _tran, _buffered, _commandTimeout, _commandType).ToList(); } finally { @@ -52,7 +52,7 @@ public async Task> QueryListAsync(string sql) try { - return (await conn.QueryAsync(sql, null, _tran)).ToList(); + return (await conn.QueryAsync(sql, null, _tran, _commandTimeout, _commandType)).ToList(); } finally { @@ -78,7 +78,7 @@ public List QueryList(string sql, DbParameter[] cmdParms) try { - return conn.Query(sql, ToDynamicParameters(cmdParms), _tran).ToList(); + return conn.Query(sql, ToDynamicParameters(cmdParms), _tran, _buffered, _commandTimeout, _commandType).ToList(); } finally { @@ -103,7 +103,7 @@ public async Task> QueryListAsync(string sql, DbParameter[] cmdParms) try { - return (await conn.QueryAsync(sql, ToDynamicParameters(cmdParms), _tran)).ToList(); + return (await conn.QueryAsync(sql, ToDynamicParameters(cmdParms), _tran, _commandTimeout, _commandType)).ToList(); } finally { diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSessionInsert.cs b/Dapper.Lite/Dapper.Lite/Session/DbSessionInsert.cs index 855db95..bdef68d 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSessionInsert.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSessionInsert.cs @@ -24,8 +24,6 @@ public void Insert(object obj) PrepareInsertSql(obj, ref strSql, ref parameters); - OnExecuting?.Invoke(strSql.ToString(), parameters); - Execute(strSql.ToString(), parameters); } @@ -40,8 +38,6 @@ public long InsertReturnId(object obj, string selectIdSql) PrepareInsertSql(obj, ref strSql, ref parameters); strSql.Append(";" + selectIdSql + ";"); - OnExecuting?.Invoke(strSql.ToString(), parameters); - object id = ExecuteScalar(strSql.ToString(), parameters); return Convert.ToInt64(id); } @@ -58,8 +54,6 @@ public async Task InsertAsync(object obj) PrepareInsertSql(obj, ref strSql, ref parameters); - OnExecuting?.Invoke(strSql.ToString(), parameters); - await ExecuteAsync(strSql.ToString(), parameters); } @@ -74,8 +68,6 @@ public async Task InsertReturnIdAsync(object obj, string selectIdSql) PrepareInsertSql(obj, ref strSql, ref parameters); strSql.Append(";" + selectIdSql + ";"); - OnExecuting?.Invoke(strSql.ToString(), parameters); - object id = await ExecuteScalarAsync(strSql.ToString(), parameters); return Convert.ToInt64(id); } @@ -105,8 +97,6 @@ public void Insert(List list, int pageSize) PrepareInsertSql(listPage, ref strSql, ref parameters, ref savedCount); - OnExecuting?.Invoke(strSql.ToString(), parameters); - Execute(strSql.ToString(), parameters); } } @@ -136,8 +126,6 @@ public async Task InsertAsync(List list, int pageSize) PrepareInsertSql(listPage, ref strSql, ref parameters, ref savedCount); - OnExecuting?.Invoke(strSql.ToString(), parameters); - await ExecuteAsync(strSql.ToString(), parameters); } } diff --git a/Dapper.Lite/Dapper.Lite/Session/DbSessionUpdate.cs b/Dapper.Lite/Dapper.Lite/Session/DbSessionUpdate.cs index 3affbdd..32cb5fc 100644 --- a/Dapper.Lite/Dapper.Lite/Session/DbSessionUpdate.cs +++ b/Dapper.Lite/Dapper.Lite/Session/DbSessionUpdate.cs @@ -69,8 +69,6 @@ public void Update(object obj) if (savedCount > 0) { - OnExecuting?.Invoke(strSql.ToString(), parameters); - Execute(strSql.ToString(), parameters); } } @@ -93,8 +91,6 @@ public async Task UpdateAsync(object obj) if (savedCount > 0) { - OnExecuting?.Invoke(strSql.ToString(), parameters); - await ExecuteAsync(strSql.ToString(), parameters); } } @@ -139,8 +135,6 @@ public void Update(List list, int pageSize) if (savedCount > 0) { - OnExecuting?.Invoke(strSql.ToString(), parameters); - Execute(strSql.ToString(), parameters); } } @@ -186,8 +180,6 @@ public async Task UpdateAsync(List list, int pageSize) if (savedCount > 0) { - OnExecuting?.Invoke(strSql.ToString(), parameters); - await ExecuteAsync(strSql.ToString(), parameters); } } diff --git a/Dapper.Lite/Dapper.Lite/Session/IDbSession.cs b/Dapper.Lite/Dapper.Lite/Session/IDbSession.cs index 0d0a910..94fe0c3 100644 --- a/Dapper.Lite/Dapper.Lite/Session/IDbSession.cs +++ b/Dapper.Lite/Dapper.Lite/Session/IDbSession.cs @@ -105,5 +105,24 @@ public partial interface IDbSession void SetTypeMap(); #endregion + #region 设置CommandType + /// + /// 设置CommandType + /// + IDbSession SetCommandType(CommandType commandType); + #endregion + + #region Dapper参数设置 + /// + /// 设置Dapper参数commandTimeout + /// + IDbSession SetCommandTimeout(int? commandTimeout); + + /// + /// 设置Dapper参数buffered + /// + IDbSession SetBuffered(bool buffered); + #endregion + } } diff --git a/Dapper.Lite/Dapper.Lite/Session/IDbSessionExecuteCommand.cs b/Dapper.Lite/Dapper.Lite/Session/IDbSessionExecuteCommand.cs index b2359af..4e5c750 100644 --- a/Dapper.Lite/Dapper.Lite/Session/IDbSessionExecuteCommand.cs +++ b/Dapper.Lite/Dapper.Lite/Session/IDbSessionExecuteCommand.cs @@ -151,6 +151,34 @@ public partial interface IDbSession #endregion + #region 查询并返回DataTable + /// + /// 执行查询语句,返回DbDataReader + /// + DataTable QueryDataTable(string sqlString); + + /// + /// 执行查询语句,返回DbDataReader + /// + Task QueryDataTableAsync(string sqlString); + + /// + /// 执行查询语句,返回DbDataReader + /// + /// 查询语句 + /// 参数 + /// IDataReader + DataTable QueryDataTable(string sqlString, DbParameter[] cmdParms); + + /// + /// 执行查询语句,返回DbDataReader + /// + /// 查询语句 + /// 参数 + /// IDataReader + Task QueryDataTableAsync(string sqlString, DbParameter[] cmdParms); + #endregion + #region 传SqlString /// diff --git a/Dapper.Lite/Dapper.Lite/SqlString/SqlString.cs b/Dapper.Lite/Dapper.Lite/SqlString/SqlString.cs index 991b4e7..01bdbba 100644 --- a/Dapper.Lite/Dapper.Lite/SqlString/SqlString.cs +++ b/Dapper.Lite/Dapper.Lite/SqlString/SqlString.cs @@ -490,6 +490,17 @@ private Dictionary GetParametersFromSql(ref string sql, bool isA Dictionary dict = new Dictionary(); MatchCollection mc = _regex.Matches(sql); int argIndex = 0; + + if (isAnonymous && mc.Count == 0) //存储过程的情况 + { + foreach (string name in anonymousValues.Keys) + { + object obj = anonymousValues[name]; + dict.Add(name, obj); + } + return dict; + } + foreach (Match m in mc) { var oldSql = m.Value; @@ -519,6 +530,7 @@ private Dictionary GetParametersFromSql(ref string sql, bool isA sql = ReplaceSql(sql, oldSql, name, parameterType); } } + return dict; } #endregion diff --git a/Dapper.Lite/Dapper.LiteTest/QueryTest.cs b/Dapper.Lite/Dapper.LiteTest/QueryTest.cs index a30d9dc..125257a 100644 --- a/Dapper.Lite/Dapper.LiteTest/QueryTest.cs +++ b/Dapper.Lite/Dapper.LiteTest/QueryTest.cs @@ -6,6 +6,7 @@ using System.Linq; using Dapper.Lite; using Utils; +using System.Data; namespace Dapper.LiteTest { @@ -446,5 +447,54 @@ public void TestDynamic3() } #endregion + #region 测试调用存储过程 + [TestMethod] + public void TestStoredProcedure() + { + var session = DapperLiteFactory.GetSession(); + + session.OnExecuting = (s, p) => + { + Console.WriteLine(s); + }; + + List list = session.SetCommandType(CommandType.StoredProcedure).Sql("query_user", new { maxId = 20 }).ToList(); + + foreach (var item in list) + { + Console.WriteLine($"{item.user_name}, {item.real_name}, {item.remark}"); + } + Assert.IsTrue(list.Count > 0); + + list = session.SetCommandType(CommandType.Text).Sql("select user_name, real_name, remark from sys_user where id<=@Id", 20).ToList(); + Assert.IsTrue(list.Count > 0); + } + #endregion + + #region 测试查询并返回DataTable + [TestMethod] + public void TestReturnDataTable() + { + var session = DapperLiteFactory.GetSession(); + + session.OnExecuting = (s, p) => + { + Console.WriteLine(s); + }; + + var sql = session.Sql("select * from sys_user where id<=@Id", 20); + DataTable dt = session.QueryDataTable(sql.SQL, sql.Params); + + foreach (DataRow dr in dt.Rows) + { + Console.WriteLine($"{dr["user_name"]}, {dr["real_name"]}, {dr["remark"]}"); + } + Assert.IsTrue(dt.Rows.Count > 0); + + var list = sql.ToList(); + Assert.IsTrue(list.Count > 0); + } + #endregion + } } diff --git a/db/procedures.sql b/db/procedures.sql new file mode 100644 index 0000000..c415e31 --- /dev/null +++ b/db/procedures.sql @@ -0,0 +1,27 @@ +/* +Navicat MySQL Data Transfer + +Source Server : localhost +Source Server Version : 50728 +Source Host : localhost:3306 +Source Database : litesql_test + +Target Server Type : MYSQL +Target Server Version : 50728 +File Encoding : 65001 + +Date: 2023-12-08 13:30:02 +*/ + +SET FOREIGN_KEY_CHECKS=0; +-- ---------------------------- +-- Procedure structure for `query_user` +-- ---------------------------- +DROP PROCEDURE IF EXISTS `query_user`; +DELIMITER ;; +CREATE DEFINER=`root`@`%` PROCEDURE `query_user`(in maxId BIGINT) +BEGIN + SELECT * from sys_user where id<=maxId; +END +;; +DELIMITER ;