将PDF / word / jpeg文件保存为sql server数据库中的图像

时间:2017-06-04 14:22:49

标签: sql-server pdf save

我正在研究现有的数据库结构。数据库具有将PDF / Word / Jpeg文件存储为图像的字段。不幸的是我无法更改数据库字段格式。

我能够成功读取存储为图像的pdf并显示pdf文件,但无法将图像插入数据库中。我尝试过许多网站的不同版本,但无法让它发挥作用。

这是我目前的代码

    Dim sql As String
    Dim da As New OleDb.OleDbDataAdapter
    Dim sqlquery As New OleDb.OleDbCommand
    Dim sqlconn As New OleDb.OleDbConnection
    Try
        sqlconn.ConnectionString = dbConnectStr1
        sqlquery.Connection = sqlconn
        sqlconn.Open()
        For cnt1 = 0 To dgv1.Rows.Count - 1
            Dim fInfo As New FileInfo(dgv1.Rows(cnt1).Cells(3).Value)
            Dim numBytes As Long = fInfo.Length
            Dim fStream As New FileStream(dgv1.Rows(cnt1).Cells(3).Value, FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(fStream)
            Dim data As Byte() = br.ReadBytes(CInt(numBytes))
            br.Close()
            fStream.Close()
            sql = "INSERT INTO " & attachmentTbl & " (DocID, DocName, DocType, DocFile, DocLength) " &
            "VALUES ('" & dgv1.Rows(cnt1).Cells(0).Value & "', '" & dgv1.Rows(cnt1).Cells(1).Value & "', '" & dgv1.Rows(cnt1).Cells(2).Value & "', "  data  ", " & numBytes & ")"
            sqlquery.CommandText = sql
            '"VALUES (@DocID, @DocName, @DocType, @DocFile, @DocLength)"

            'sqlquery.OleDbParameter.Add(New System.Data.SqlClient.SqlParameter("@DocID", dgv1.Rows(cnt1).Cells(0).Value))
            'sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@DocName", dgv1.Rows(cnt1).Cells(1).Value))
            'sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@DocType", dgv1.Rows(cnt1).Cells(2).Value))
            'sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@DocFile", data))
            'sqlquery.Parameters.Add(New System.Data.SqlClient.SqlParameter("@DoDocLength", numBytes))
            'sqlquery.ExecuteNonQuery()
        Next
        sqlconn.Close()
    Catch ex As Exception
        errorFlag = True
        MessageBox.Show("Error#060 " & ex.Message)
        Exit Sub
    End Try

此外,有没有办法让Windows资源管理器中显示文件类型。即对于.pdf文件显示“adobe acrobat document”.xls文件将是“Microsoft excel工作表”

我尝试使用以下代码,但我没有得到上述内容

    extn = Path.GetExtension(txtFileName.Text)
    Dim regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extn)
    If Not regKey Is Nothing Then
        Dim ct As Object = regKey.GetValue("Content Type")
        If Not ct Is Nothing Then
            extn = ct.ToString()
        End If
    End If

表达你的帮助。

0 个答案:

没有答案