问题描述
将图片从PictureBox1保存到SQL Server数据库时遇到问题,我进行了研究,发现我必须将图片转换为字节数组才能执行此操作,但是我不知道该怎么做.在我尝试编辑的代码中应用它.当我单击保存按钮时,出现此错误:No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type
.
I have a problem saving images from PictureBox1 to my SQL server database, I've done my research and found out that I have to convert my image to a byte array in order to do that but I don't know how to apply it in this code that I'm trying to edit. When I click on the save button I get this error: No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type
.
我认为这与将图像插入数据库有关,因此我将向您展示与之相关的代码片段.
I think it has something to do with inserting the image to the database so I'll show you code snippets that are related to it.
这是具有其属性的类:
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 .
In SQL SERVER database have datatype IMAGE. Keep the column datatype as IMAGE.
您可以使用以下示例代码并将Image传递给以下函数,该函数将图像转换为字节以存储在数据库中.
You can use the following sample code and pass Image to the below function which converts the image to byte to store in database.
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;
}
这篇关于从对象类型System.Drawing.Bitmap到已知的托管提供程序本机类型VB.NET不存在映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!