OrmLite抛出未知错误提供给命令的参数不足

时间:2017-04-25 19:53:34

标签: c# sqlite ormlite-servicestack

我正在使用ServiceStack.OrmLite v4.0.62(.NET Framework 4.0的最后一个)。我使用SQLite数据库。所以,在我的UI上,我有一个包含搜索字段的表单。根据用户设置的条件,我动态生成Expression<Func<T, bool>>。然后我的函数有以下视图:

/// <summary>
/// Gets the number of records in table corresponding the specified criteria
/// </summary>
/// <param name="predicate">Search criteria</param>
/// <returns></returns>
public virtual long CountByCondition(Expression<Func<T, bool>> predicate)
{
    // bug: OrmLite !string.IsNullOrWhiteSpace(someStringValue) function causes error while building predicate

    var q = Db.Connection.From<T>();
    q = q.Where(predicate);
    string cnt = q.ToCountStatement();
    // here we have an Exception
    return Db.Connection.Scalar<long>(cnt, q.Params);
    //return Db.Connection.Count<T>(predicate);
}

我知道错误行(对于OrmLite开发人员),它与当前问题无关。 例如,q.Params包含名称从“@0”到“@10”的11个参数。 但是让我们看看SQL已生成:

SELECT COUNT(*) 
FROM "Person"
WHERE ("IsTer" = @0) AND 
       upper("Pib") like @1 AND 
       "Id" IN (SELECT DISTINCT "PersonId" 
                FROM "Predmet"
                WHERE ("PType" = @2 AND 
                      (upper("PValue") like @3 OR upper("PValue") like @4 OR upper("PValue") like @5 OR upper("PValue") like @6 OR upper("PValue") like @7 OR upper("PValue") like @8)) AND 
                      ("PType" = @9 AND upper("PValue") like @30)

您是否看到参数名称中存在一些不匹配,即最后一个参数名称为“@30”! 我认为这就是为什么命令抛出异常的原因是完整的堆栈:

"unknown error Insufficient parameters supplied to the command";
"System.Data.SQLite.SQLiteException";"   
   в System.Data.SQLite.SQLiteStatement.BindParameter(Int32 index, SQLiteParameter param)
   в System.Data.SQLite.SQLiteStatement.BindParameters()
   в System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   в System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   в System.Data.SQLite.SQLiteDataReader.NextResult()
   в System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   в System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   в System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior)
   в ServiceStack.OrmLite.OrmLiteCommand.ExecuteReader()
   в ServiceStack.OrmLite.OrmLiteReadCommandExtensions.ExecReader(IDbCommand dbCmd, String sql)
   в ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.Scalar[T](IDbCommand dbCmd, String sql)
   в ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.Scalar[T](IDbCommand dbCmd, String sql, IEnumerable`1 sqlParams)
   в ServiceStack.OrmLite.OrmLiteReadApi.<>c__DisplayClass3f`1.<Scalar>b__3e(IDbCommand dbCmd)
   в ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)
   в ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func`2 filter)
   в ServiceStack.OrmLite.OrmLiteReadApi.Scalar[T](IDbConnection dbConn, String sql, IEnumerable`1 sqlParams)
   в Reestr.DAL.Repositories.Repository`1.CountByCondition(Expression`1 predicate) в d:\Project\Reestr\Reestr.DAL\Repositories\Repository.cs:строка 113
   в Reestr.DAL.Repositories.PersonRepository.CountByCondition(Expression`1 predicate) в d:\Project\Reestr\Reestr.DAL\Repositories\PersonRepository.cs:строка 47
   в Reestr.BLL.Services.PersonService.GetDataBy(Expression`1 predicate, Int32 pageNumber, Int32 pageSize) в d:\Project\Reestr\Reestr.BLL\Services\PersonService.cs:строка 202
   в Reestr.WinForms.Views.FrmMain.BindGrid(Int32 pageIndex, Expression`1 predicate, SortInfo`1 sortInfo) в d:\Project\Reestr\Reestr.WinForms\Views\FrmMain.cs:строка 427"

那么,请你能帮我解决这个问题或解释原因吗?我是否需要手动更改生成的SQL?我认为这不是解决当前问题的好选择。

已更新

今天我尝试生成另一个搜索表达式,这是OrmLite生成的SQL:

SELECT COUNT(*) 
FROM "Person"
WHERE ("IsTer" = @0 OR "IsTax" = @1) AND 
       upper("Pib") like @2 AND 
       "Bd" > @3 AND 
      upper("Inn") like @4 AND 
      upper("Bp") like @5 AND 
      upper("Lp") like @6 AND 
      upper("Doc") like @7 AND 
      upper("Gr") like @8 AND 
      upper("Org") like @9 AND 
      upper("Pseudo") like @10 AND 
      "Id" IN (SELECT DISTINCT "PersonId" 
               FROM "Predmet"
               WHERE ("PType" = @11 AND 
                     (upper("PValue") like @12 OR upper("PValue") like @123 OR upper("PValue") like @124 OR upper("PValue") like @125 OR upper("PValue") like @126)) AND 
                     ("PType" = @127 AND upper("PValue") like @128) AND 
                     ("PType" = @129 AND upper("PValue") like @1230) AND 
                     ("PType" = @1231 AND upper("PValue") like @1232 escape '^') AND 
                     ("PType" = @1233 AND upper("PValue") like @1234) AND 
                     ("PType" = @1235 AND upper("PValue") like @1236)

我无法理解发生了什么。我们看到带有奇怪名称的参数,当然在q.Params集合......

中没有带有这些名称的参数

1 个答案:

答案 0 :(得分:0)

这是在最新版本中修复的......