【发布时间】:2019-09-10 11:50:55
【问题描述】:
我有以下代码:
ActiveModule resultObject = null;
StringBuilder sqlStringBuilder = new StringBuilder();
sqlStringBuilder.AppendLine("SELECT * FROM suite_activemodule as _module");
sqlStringBuilder.AppendLine("WHERE CityID = @CityID AND BaseModuleID = @BaseModuleID");
using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
var _resultList = connection.Query<ActiveModule>(sqlStringBuilder.ToString(), new { CityID = cityID, ModuleID = moduleID }, null, true, null, null); //<- This is were I get the error...
int listSize = _resultList.Count();
if (_resultList != null && !_resultList.Count().Equals(0))
{
resultObject = _resultList.ToList().First();
resultObject.Module = BaseModuleRepository.GetByID(resultObject.ModuleID);
}
}
查询本身运行良好,但一旦我尝试运行查询,我就会收到以下错误消息:
北MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()
北MySql.Data.MySqlClient.MySqlConnection.Abort()
北 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为)
bei Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed,CommandBehavior 行为)在 C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 1053.
bei Dapper.SqlMapper.d__138`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 1081.
贝 System.Collections.Generic.List
1..ctor(IEnumerable1 集合)bei System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
bei Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object 参数,IDbTransaction 事务,布尔缓冲,Nullable
1 commandTimeout, Nullable1 commandType)在 C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 723....Repository.GetByCityIDAndModuleID(Int64 cityID, Int64 moduleID)
...DeactivateModule(字符串模块ID)
但是我之前运行过多个这样的查询并且没有收到此错误。这没有多大意义,因为使用过的东西都不是真正的空值。
我已经通过 Visual Studio 进行了干净的构建和完整的构建。
有人知道这个问题并知道如何解决吗?
【问题讨论】:
-
不确定这里是否有一定的重要性,但你没有打开连接
-
另外,Query
的最后四个参数是可选的,您传递的是默认值。您可以省略它们。 -
如何设置'@BaseModelID' SQL 参数?看来您只传入 CityID 和 ModuleID
-
@Steve 连接在内部被 dapper 打开。
-
@SteveWong 就是这样。它无法设置值。我现在已经更改了名称并且它起作用了。谢谢。