如何使用DatabaseFactory将varbinary(MAX)从SQL Server中拉出来?

时间:2014-12-09 15:04:25

标签: vb.net linq sql-server-2008-r2 binaryfiles

我在SQL Server中有一个视图,它为扫描的应用程序表单的PDF副本提供varbinary(MAX)数据(不要问)。视图中的列名称包括[FileBinary],[Size]和[OriginalFileName]。

我在整个项目中使用DatabaseFactory来提取数据,直到现在还没有类型问题。从这个特定视图获取数据的代码是:

Public Shared Function GetAppFormPDF(studentRefNo As String) As List(Of ProSolutionReportsAppForm)
    Dim db = DatabaseFactory.CreateDatabase(Constants.DataBaseConnectionStringProSolutionReports)
    Dim sql As String = String.Format("SELECT * FROM [ProSolutionReports].[dbo].[MC_AppFormSSRS_FromPDFScan] WHERE RefNo LIKE '{0}%'", studentRefNo.Trim())
    Return db.ExecuteSqlStringAccessor(Of ProSolutionReportsAppForm)(sql).ToList()
End Function

它被拉入的模型被声明为:

Public Property FileBinary As System.Data.Linq.Binary
Public Property Size As Long
Public Property OriginalFileName As String

当我点击GetAppFormPDF()时,我被告知“列'FileBinary'的值无法转换为'System.Data.Linq.Binary'类型的属性'FileBinary'。”

我为Linq DataContext创建了一个SQL来查看输入是如何在那里完成的,但是(非常明显地)也输入了System.Data.Linq.Binary,并且该工作正常。我也尝试输入FileBinary作为Byte(),它从表的其余部分给我数据,但设置FileBinary = Nothing。

如何将varbinary(MAX)数据导入模型?

1 个答案:

答案 0 :(得分:0)

经过4个小时的调查后,我认为DataFactory是错误的解决方法。相反,我是按照我应该拥有的方式做到的:

    Dim appForms = New List(Of ProSolutionReportsAppForm)

    Dim conn = New SqlConnection(ConfigurationManager.ConnectionStrings(Constants.DataBaseConnectionStringProSolutionReports).ConnectionString)
    Dim sql As String = "SELECT * FROM [ProSolutionReports].[dbo].[MC_AppFormSSRS_FromPDFScan] WHERE RefNo = @RefNo"

    Dim command As New SqlCommand(sql, conn)
    command.Parameters.AddWithValue("@RefNo", studentRefNo.Trim())
    conn.Open()

    Dim reader As SqlDataReader = command.ExecuteReader()
    While reader.Read()
        appForms.Add(New ProSolutionReportsAppForm With {.FileBinary = reader("FileBinary"), _
                                                         .Size = reader("Size"), _
                                                         .ApplicationID = reader("ApplicationID"), _
                                                         .RefNo = studentRefNo,
                                                         .FirstForename = reader("FirstForename"), _
                                                         .Surname = reader("Surname"), _
                                                         .OriginalFileName = reader("OriginalFileName")})
    End While

    reader.Close()
    conn.Close()

    Return appForms