【问题标题】:Using Dapper with BLOBs and SQL Server CE将 Dapper 与 BLOB 和 SQL Server CE 一起使用
【发布时间】:2012-03-26 22:26:42
【问题描述】:

当使用超过 8000 字节数据的 BLOB 时,需要专门设置 Parameter.SqlDbType = SqlDbType.Image 使其工作(as explained here)。

Dapper 在看到 byte[] 字段时,默认为 SqlDbType.Binary,这意味着对于较大的 blob,插入和更新将失败并出现数据截断错误。

这个问题有没有优雅的解决方案?我能看到的唯一选择是使用 ADO.NET 方法对整个事务进行编码。

【问题讨论】:

  • 如果您查看 dapper 源 (code.google.com/p/dapper-dot-net/source/browse/Dapper/…),您可以在函数“LookupDbType”中添加一个特殊情况,返回适当的 SqlDbType。不知道它是否会破坏其他东西。
  • 谢谢!我想我会修改静态 SqlMapper 构造函数并修改以下行: typeMap[typeof(byte[])] = DbType.Binary;
  • 哦,这很痛苦;不过,我怀疑您可能最好将此记录为针对 dapper 的错误

标签: sql-server-ce blob dapper


【解决方案1】:

我遇到了同样的问题。解决如下:

private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transaction, 
                                       string sql, Action<IDbCommand, object> paramReader, 
                                       object obj, int? commandTimeout, 
                                       CommandType? commandType)
{
    var cmd = cnn.CreateCommand();
    var bindByName = GetBindByName(cmd.GetType());
    if (bindByName != null) bindByName(cmd, true);
    if (transaction != null)
        cmd.Transaction = transaction;
    cmd.CommandText = sql;
    if (commandTimeout.HasValue)
        cmd.CommandTimeout = commandTimeout.Value;
    if (commandType.HasValue)
        cmd.CommandType = commandType.Value;
    if (paramReader != null)
    {
        paramReader(cmd, obj);
    }
    //CODTEC SISTEMAS
    foreach (System.Data.SqlServerCe.SqlCeParameter item in cmd.Parameters)
    {
        if (item.SqlDbType == System.Data.SqlDbType.VarBinary)
            item.SqlDbType = System.Data.SqlDbType.Image;
    }
    //CODTEC SISTEMAS
    return cmd;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多