【问题标题】:Database.ExecuteSprocAccessor() not mapping blob data correctlyDatabase.ExecuteSprocAccessor() 未正确映射 blob 数据
【发布时间】:2012-06-26 14:18:27
【问题描述】:

如果有人能帮我解决这个问题,我将不胜感激。对于初学者,我有这样的课程:

public class Blob
{
    public int BlobID { get; set; }
    public string BlobName { get; set; }
    public string FileName { get; set; }
    public string FileMimeType { get; set; }
    public int FileSize { get; set; }
    public byte[] FileContent{ get; set; }
    public DateTime DateCreated { get; set; }
    public int CreatedByProfileID { get; set; }
}

非常标准,它只是一个映射到具有完全相同字段名称的表的对象。 SQL Server 中的表如下所示:

我的控制器具有添加和查看操作来读取和写入数据库。我可以很好地编写文件,使用下面的操作代码:

[HttpPost]
public ActionResult Add(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        Database db = DatabaseFactory.CreateDatabase("dbconnstr");

        byte[] fileContent = new byte[file.ContentLength];
        file.InputStream.Read(fileContent, 0, file.ContentLength);

        object[] paramaters = 
        {
            file.FileName,
            file.FileName,
            file.ContentType,
            file.ContentLength,
            fileContent,
            DateTime.Now,
            12518
        };

        db.ExecuteNonQuery("sp_Blob_Insert", paramaters);
    }

    return RedirectToAction("Index");
}

但是当我使用下面的查看操作代码将文件读取到浏览器时,FileContent 字段始终为空:

public ActionResult View(int id)
{
    Database db = DatabaseFactory.CreateDatabase("dbconnstr");

    Blob blob = db.ExecuteSprocAccessor<Blob>("sp_Blob_SelectByPkValue", id).Single();

    return File(blob.FileContent, blob.FileMimeType, blob.FileName);
}

但是,如果我专门映射字段名称,它会起作用:

public ActionResult View(int id)
{
    Database db = DatabaseFactory.CreateDatabase("dbconnstr");

    IRowMapper<Blob> mapper = MapBuilder<Blob>.MapAllProperties().MapByName(x => x.FileContent).Build();

    Blob blob = db.ExecuteSprocAccessor<Blob>("sp_Blob_SelectByPkValue", mapper, id).Single();

    return File(blob.FileContent, blob.FileMimeType, blob.FileName);
}

这是 ExecuteSprocAccessor() 函数的错误吗?我做错了什么吗?

提前感谢您的宝贵时间。

【问题讨论】:

  • 你找到答案了吗?我也有同样的问题
  • 我没有,而且我在谷歌上搜索也没有运气......

标签: c# asp.net-mvc-3 sql-server-2008 enterprise-library-5 data-access-app-block


【解决方案1】:

您可以使用以下代码:

.Map(x => x.FileContent).WithFunc(ConvertVarBinaryToByteArray); 

然后像这样创建函数:

private static byte[] ConvertVarBinaryToByteArray(IDataRecord dataRecord) 
{ 
    return (byte[]) dataRecord.GetValue(dataRecord.GetOrdinal("FileContent")); 
} 

【讨论】:

    【解决方案2】:

    我是这样解决问题的:

    在执行 ExecuteSprocAccessor 之前添加此代码:

    IRowMapper<FileEmail> rowMapper = MapBuilder<FileEmail>.MapAllProperties()
            .Map(x => x.MyFile)
            .WithFunc(ConvertVarBinaryToByteArray).Build();    
    

    然后创建上面 Russ 所说的方法:

    private static byte[] ConvertVarBinaryToByteArray(IDataRecord dataRecord)
        {
            return (byte[])dataRecord.GetValue(dataRecord.GetOrdinal("FileContent"));
        } 
    

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 2016-08-07
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多