【问题标题】:Error: System.Data.Linq.Binary' cannot be converted to '1-dimensional array of Byte'错误:System.Data.Linq.Binary' 无法转换为 '一维字节数组'
【发布时间】:2010-10-16 18:50:22
【问题描述】:

我正在尝试使用 linq 从数据库返回二进制文件以在浏览器中显示。下面使用 ado.net 的方法有效,但我试图 ypgrade 到 linq 但 linq 版本返回错误。

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) 
    Dim imageId As String = context.Request.QueryString("id")
    Dim ret As DataTable = Nothing
    ret = DPGetImageData.GetImageById(Convert.ToInt64(imageId))

        For Each dt As DataRow In ret.Rows
            For Each c As DataColumn In ret.Columns
                If Not (dt(c) Is Nothing) Then
                    context.Response.Clear()
                    context.Response.BufferOutput = False
                    context.Response.OutputStream.Write(CType(dt.Table.Rows(0).Item("imageData"), Byte()), 0, CInt(dt.Table.Rows(0).Item("imageSize")))
                    context.Response.End()
                End If

            Next
        Next


End Sub

工作 Linq 版本:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    If Not String.IsNullOrEmpty(Current.Request.QueryString("Id")) Then
        Dim imageId = HttpContext.Current.Request.QueryString("Id")
        Dim result = repository.FindById(Convert.ToInt64(imageId)).imageData.ToArray
        HttpContext.Current.Response.BinaryWrite(result)
        context.Response.End()

    End If
End Sub

【问题讨论】:

    标签: vb.net arrays binary n-dimensional


    【解决方案1】:

    您应该可以只调用 Response.BinaryWrite(result.ToArray())。注意括号,ToArray 是一个方法调用。

    【讨论】:

    • 很好,不过我不懂VB :)
    【解决方案2】:

    您应该不需要强制转换为 Binary.ToArray,已经返回一个字节数组。

    另一种方法是直接使用字节数组。您可以在设计器中对其进行修改。

    更新:

    我不完全确定,但可能是您的 DataContext 已被释放。我认为 Binary 类型使用延迟加载。

    如果你添加一个堆栈跟踪,我们可以看看是不是这样。

    【讨论】:

    • 我一直得到一个 nullReference,因为数据不在模型中。我只是不知道对大文件会有多大的偏好。我希望有人会谈到这一点。我还发现了这篇文章tinyurl.com/ck39un它为我填补了更多的空白。
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多