2012-03-09 16 views
2

使用超過8000字節數據的BLOB時,需要專門設置Parameter.SqlDbType = SqlDbType.Image以使其工作(as explained here)。將Dapper與BLOB和SQL Server配合使用CE

Dapper,當它看到一個byte[]字段時,默認爲SqlDbType.Binary,這意味着對於較大的blob,插入和更新將失敗,並出現數據截斷錯誤。

有沒有一個優雅的解決方案來解決這個問題?我只能看到的選擇是用ADO.NET方法編寫整個事務。

+0

如果你看一看短小精悍源(http://code.google.com/p/dapper-dot-net/source/browse/Dapper/SqlMapper.cs)你可以在函數「LookupDbType」中添加一個返回適當的SqlDbType的特例。沒有線索,如果它打破別的東西。 – Alex 2012-03-09 13:41:41

+0

謝謝! 我想我會修改靜態SqlMapper的構造函數並修改以下行: typeMap [typeof(byte [])] = DbType.Binary; – Sameera 2012-03-10 07:57:20

+2

哦,這是一種痛苦;儘管如此,我懷疑你可能會更好地把它當作一個針對短小精靈的bug – 2012-03-11 20:05:41

回答

0

我有同樣的問題。解決如下:

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