将Dapper与BLOB和SQL Server CE结合使用 [英] Using Dapper with BLOBs and SQL Server CE

查看:327
本文介绍了将Dapper与BLOB和SQL Server CE结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When using BLOBs with more than 8000 bytes of data, you need to specifically set Parameter.SqlDbType = SqlDbType.Image to make it work (as explained here).

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

Dapper, when it sees a byte[] field, defaults to a SqlDbType.Binary, which means for larger blobs, the inserts and updates will fail with a data truncation error.

是否有解决此问题的简便方法?我唯一看到的选择是使用ADO.NET方法对整个事务进行编码.

Is there an elegant solution to this problem? Only option I can see is to code the entire transaction with ADO.NET methods.

推荐答案

我遇到了同样的问题.解决方法如下:

I had the same problem. Resolved as follows:

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

这篇关于将Dapper与BLOB和SQL Server CE结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆