sql-server - 不存在从对象类型 System.Drawing.Bitmap 到已知托管提供程序 native 类型 VB.NET 的映射

标签 sql-server database vb.net bitmap picturebox

我在将图像从 PictureBox1 保存到我的 SQL 服务器数据库时遇到问题,我已经完成研究并发现我必须将我的图像转换为字节数组才能执行此操作,但我不知道该怎么做将它应用到我正在尝试编辑的这段代码中。当我单击保存按钮时,出现此错误:不存在从对象类型 System.Drawing.Bitmap 到已知托管提供程序 native 类型的映射

我认为它与将图像插入数据库有关,因此我将向您展示与之相关的代码片段。

这是一个类及其属性:

Friend Class PersonFile
    Private _NewID As String

 Private _PersonID As String
    Friend Property PersonID() As String
        Get
            Return _PersonID
        End Get
        Set(ByVal Value As String)
            _PersonID = Value
        End Set
    End Property
 Private _Photo As Image
    Friend Property Photo() As Image
        Get
            Return _Photo
        End Get
        Set(ByVal Value As Image)
            _Photo = Value
        End Set
    End Property

这是插入函数:

Friend Class PersonFileDB
 Friend Function DXInsertFile(ByVal cItem As PersonFile) As PersonFile
        Dim cReturn As New PersonFile

 Using oleCON As New SqlConnection(AppVariables.GConnectionString)
            oleCON.Open()

 Dim n1 As String = ""
            n1 = CreateNewID()
            cItem.PersonID = n1

            Dim xSQL As New StringBuilder
            xSQL.AppendLine(" INSERT INTO PersonData ")
            xSQL.AppendLine("( ")
            xSQL.AppendLine(" PersonID, ")
            xSQL.AppendLine(" Photo, ")
            ''Other code...
            xSQL.AppendLine("VALUES ( ")
            xSQL.AppendLine(" @PersonID, ")
            xSQL.AppendLine(" @Photo, ")
            ''Other code...

            Dim oleComm As New SqlCommand(xSQL.ToString, oleCON)
            With oleComm.Parameters
                .AddWithValue(" @PersonID, ", cItem.PersonID)
                .AddWithValue("@Photo", cItem.Photo)
            ''Other code...
            End With

            Dim n As Integer
            n = oleComm.ExecuteNonQuery()
            If n <> 0 Then
                cItem.Updated = True
                SaveNewID(CInt(cItem.PersonID))
                cReturn = cItem
            End If
 End Using
 Return cReturn
End Function

这是点击事件:

Private CurrPerson As New PersonFile

Private Sub cmdPersonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPersonSave.Click

        CurrPerson.Photo = PictureBox1.Image
        ''Other code...
  Dim cdb As New PersonFileDB

        Select Case cmdPersonSave.Text
            Case "Add"
                UIClear()
                UISetControls(False)
                cmdPersonSave.Text = "Save"
                cmdPersonUpdate.Text = "Cancel"
                cmdPersonDelete.Enabled = False
                cmdSearch.Enabled = False
            Case "Save"
                If EditMode Then
                    CurrPerson = cdb.DXUpdateFile(CurrPerson)
                    EditMode = False
                Else
                    CurrPerson = cdb.DXInsertFile(CurrPerson)
                End If
                If CurrPerson.Updated Then
                    BindGrid(CurrPerson.PersonID)
                    UISetControls(True)
                End If
                cmdPersonSave.Text = "Add"
                cmdPersonUpdate.Text = "Edit"
                cmdPersonDelete.Enabled = True
                cmdSearch.Enabled = True
                UISetControls(True)
        End Select
        End Sub

最佳答案

在 SQL SERVER 数据库中,数据类型为 IMAGE。将列数据类型保留为 IMAGE

您可以使用以下示例代码并将图像传递给下面的函数,该函数将图像转换为字节以存储在数据库中。

public static byte[] ImageToByte2(Image img)
        {
            byte[] byteArray = new byte[0];
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                stream.Close();

                byteArray = stream.ToArray();
            }
            return byteArray;
        }

关于sql-server - 不存在从对象类型 System.Drawing.Bitmap 到已知托管提供程序 native 类型 VB.NET 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310874/

相关文章:

vb.net - 如何在 Visual Basic 中定义可空类型属性

sql-server - Delphi:使用参数时如何获取传递到服务器的查询

sql - SQL Server : Output data frame into a table 中的 R

mysql - "Most Viewed"特征数据库的实现

oracle - 从oracle中的clob中提取一个子串

使用 HttpWebRequest.Create 时的 C# 与 VB 语法

sql-server - SQL : add new rows for each distinct value

php - SQL Server 的 ODBC 驱动程序 18]SSL 提供程序 : [error:1416F086]

database - 我应该如何在 redshift 数据库中删除多个列?

vb.net - 获取远程文件大小