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

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

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

最佳答案

我有同样的问题。解决如下:

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;
}

09-06 08:08