我的表单中有一个按钮和一个datagridview。我需要从数据库中获取图像并在datagrid中显示其值,然后在datagrid中获取图像的值以将其复制到另一个数据库。
具体来说,我将图像(blob)从database1的table1复制到database2的表1。
的button1_Click:
Dim img As Image
Dim bArr As Byte()
Try
Dim Sql = "Select ID, IMG from sample"
connectionOnline()
Dim cmd = New MySqlCommand(Sql, ConOnline)
Dim dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
DataGridView1.Rows.Clear()
While dr.Read = True
img = dr(1)
bArr = imgToByteArray(img)
DataGridView1.Rows.Add(dr(0), bArr)
End While
ConOnline.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
connectionSync()
Dim a, b As String
Dim Sql = "INSERT INTO SAMPLE (ID, IMG)values(@a,@b)"
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
a = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
Dim img1 As Image = byteArrayToImage(bArr)
Dim cmd As New MySqlCommand(Sql, ConSync)
cmd.Parameters.AddWithValue("@a", a)
cmd.Parameters.AddWithValue("@b", img1)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
connectionSync()
Dim Sql = "INSERT INTO B.SAMPLE(ID, IMG) SELECT ID, IMG FROM C.SAMPLE WHERE not exists (SELECT 1 from B.SAMPLE WHERE B.SAMPLE.ID=C.SAMPLE.ID)"
Dim cmd = New MySqlCommand(Sql, ConSync)
With cmd
.ExecuteNonQuery()
End With
MsgBox("Success", vbInformation, "Save")
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
connectionOffline()
Dim Sql = "UPDATE SAMPLE SET IMG=(SELECT C.SAMPLE.NAME FROM C.SAMPLE WHERE C.SAMPLE.ID=B.SAMPLE.ID) WHERE B.SAMPLE.ID=(SELECT C.SAMPLE.ID FROM C.SAMPLE WHERE C.SAMPLE.ID=B.SAMPLE.ID)"
Dim cmd = New MySqlCommand(Sql, ConOffline)
With cmd
.ExecuteNonQuery()
End With
MsgBox("Success")
ConOffline.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
B和C是数据库
虽然样本是表格
下面是我用来转换图像的功能
Public Function imgToByteArray(ByVal img As Image) As Byte()
Using mStream As New MemoryStream()
img.Save(mStream, img.RawFormat)
Return mStream.ToArray()
End Using
End Function
Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Using mStream As New MemoryStream(byteArrayIn)
Return Image.FromStream(mStream)
End Using
End Function
结果(在数据库中; IMG)仅显示:" System.Drawing.Bitmap "而不是实际的图像
答案 0 :(得分:0)
您只存储“Image.ToString”。
尝试使用二进制参数:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void ret_2darr(char *(*str)[5])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<=5;j++)
{
str[i][j]=(char *)malloc(sizeof("sach"));
strcpy(str[i][j],"sach");
}
}
}
main()
{
char *(*a)[5]=NULL;
ret_2darr(a);
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<=5;j++)
{
printf("%s",a[i][j]);
}
}
}