如何通过VB net将多个图像(blob)从SQL表保存到图像文件? [英] How can I save multiple image (blob) from SQL table to image file by VB net?

查看:139
本文介绍了如何通过VB net将多个图像(blob)从SQL表保存到图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb net 2017.



通过这个下一个代码,我从sql表中获取请求IMAGE(归档名称为MMOBJS.LNL_BLOB)



 OpenConnection()

cmd = SqlCommand( 选择badge.ID,emp.SSNO,emp.LASTNAME,emp.FIRSTNAME,emp.MIDNAME,BADGSTAT.NAME,DEPT。 NAME,BADGE.ACTIVATE,BADGE.DEACTIVATE,MMOBJS.LNL_BLOB来自EMP内部联接BADGE EMP.ID = BADGE.EMPID INNER JOIN UDFEMP ON UDFEMP.ID = EMP.ID INNER JOIN DEPT UDFEMP.DEPT = DEPT.ID INNER JOIN BADGESTAT ON BADGE.STATUS = BADGSTAT.ID INNER JOIN MMOBJS ON MMOBJS.EMPID = BADGE.EMPID其中BADGE.ID喜欢'& DataGridView1.CurrentRow.Cells( 0 )。Value.ToString& %',connections)
reader = cmd.ExecuteReader()





我尝试过:



和下一个代码,我可以为一个选定的记录保存一个图像。



 如果 reader.Read =  False  然后 
MessageBox.Show( Фотодляпропуска№ &安培; DataGridView1.CurrentRow.Cells( 0 )。Value.ToString& нетвбазе。 消息,MessageBoxButtons.OK,MessageBoxIcon.Information)
否则

' 以字节为单位存储数据库中的图像值
Dim imageData 作为 字节()= reader( 9
如果 imageData 没什么 然后
使用 ms 作为 MemoryStream(imageData, 0 ,imageData.Length)
ms.Write(imageData, 0 ,imageData.Length)
pic_box.Image = Image.FromStream(ms, True

Dim 路径 As String = C:\ tmp2 \
Dim Dir As String = System .IO.Path.GetDirectoryName(路径)

尝试
尝试
如果 System.IO.Directory.Exists(Dir)然后
System.IO.Directory.CreateDirectory(Dir)
结束 如果

使用 sfdpic
。标题= 将图像保存为
.Filter = Jpg,Jpeg Images | * .jpg; * .jpeg
.AddExtension = True
.DefaultExt = 。jpg
' .FileName = txt_ssno.Text ++ txt_lname.Text ++ txt_fname.Text ++ txt_midname.Text
.FileName = DataGridView1.CurrentRow.Cells( 1 )。Value.ToString + + DataGridView1.CurrentRow.Cells( 2 )。Value.ToString + + DataGridView1.CurrentRow.Cells( 3 )。Value.ToString + + DataGridView1.CurrentRow.Cells( 4 )。Value.ToString
.OverwritePrompt = True
.InitialDirectory = Dir
.RestoreDirectory = True

如果 .ShowDialog = DialogResult.OK 那么
如果 .FilterIndex = 1 那么
pic_box.Image.Save(sfdpic.FileName,Imaging.ImageFormat.Jpeg)
结束 如果
结束 如果
结束 使用

Catch ex As 异常
MessageBox.Show( 错误:保存图像失败 - >>& ex.Message.ToString())

最后
sfdpic.Dispose()
结束 尝试

Catch ex As Exception
Me .Cursor = Cursors。 Default
MessageBox.Show(ex.Message, 消息,MessageBoxButtons.OK ,MessageBoxIcon.Information)
结束 尝试

结束 使用
结束 如果
reader.Close()
Cursor = Cursors。 D efault
结束 如果
Cursor = Cursors。默认

尝试







我无法为过滤的datagridview列表保存多个图像。我怎么能这样做?

请给我一个例子。

解决方案

我立刻发现了两个错误。

不幸的是,VB不是我的主要语言,目前我无法访问IDE。您将需要自己解决语法中的任何错误。



问题#1:不要将SQL命令连接在一起

使用参数化查询来消除SQL注入的机会

修复:更改WHERE子句

 '  BAD: 
...其中BADGE.ID喜欢'& DataGridView1.CurrentRow.Cells( 0 )。Value.ToString& %'

' 好:
...其中BADGE.ID = @BadgeID
cmd.Parameters.AddWithValue( @BadgeID,DataGridView1.CurrentRow.Cells( 0 )。值);





问题#2:只检索到1个值

修复:循环读取器



 如果 reader.HasRows =  False  然后 
MessageBox.Show( 错误消息
Else
while reader.Read()
' <跨度class =code-comment>此处为读取器对象中的每一行执行代码
结束 虽然
结束 如果


I am using vb net 2017.

By this next code i get request IMAGE from sql table (filed name MMOBJS.LNL_BLOB)

     OpenConnection()

     cmd = New SqlCommand("Select badge.ID , emp.SSNO , emp.LASTNAME, emp.FIRSTNAME, emp.MIDNAME, BADGSTAT.NAME, DEPT.NAME , BADGE.ACTIVATE, BADGE.DEACTIVATE, MMOBJS.LNL_BLOB  FROM EMP INNER JOIN BADGE ON EMP.ID = BADGE.EMPID INNER JOIN UDFEMP ON UDFEMP.ID = EMP.ID INNER JOIN DEPT on UDFEMP.DEPT = DEPT.ID INNER JOIN BADGSTAT ON BADGE.STATUS = BADGSTAT.ID INNER JOIN MMOBJS ON MMOBJS.EMPID = BADGE.EMPID where BADGE.ID Like '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "%'", connections)
reader = cmd.ExecuteReader()



What I have tried:

and by next code, i can save one image for one selected record.

If reader.Read = False Then
     MessageBox.Show("Фото для пропуска № " & DataGridView1.CurrentRow.Cells(0).Value.ToString & " нет в базе.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else

     'store image value from the database in byte
     Dim imageData As Byte() = reader(9)
     If Not imageData Is Nothing Then
         Using ms As New MemoryStream(imageData, 0, imageData.Length)
             ms.Write(imageData, 0, imageData.Length)
             pic_box.Image = Image.FromStream(ms, True)

             Dim Path As String = "C:\tmp2\"
             Dim Dir As String = System.IO.Path.GetDirectoryName(Path)

             Try
                 Try
                     If Not System.IO.Directory.Exists(Dir) Then
                         System.IO.Directory.CreateDirectory(Dir)
                     End If

                     With sfdpic
                         .Title = "Save image As"
                         .Filter = "Jpg, Jpeg Images|*.jpg; *.jpeg"
                         .AddExtension = True
                         .DefaultExt = ".jpg"
                         '.FileName = txt_ssno.Text + " " + txt_lname.Text + " " + txt_fname.Text + " " + txt_midname.Text
                         .FileName = DataGridView1.CurrentRow.Cells(1).Value.ToString + " " + DataGridView1.CurrentRow.Cells(2).Value.ToString + " " + DataGridView1.CurrentRow.Cells(3).Value.ToString + " " + DataGridView1.CurrentRow.Cells(4).Value.ToString
                         .OverwritePrompt = True
                         .InitialDirectory = Dir
                         .RestoreDirectory = True

                         If .ShowDialog = DialogResult.OK Then
                             If .FilterIndex = 1 Then
                                 pic_box.Image.Save(sfdpic.FileName, Imaging.ImageFormat.Jpeg)
                             End If
                         End If
                     End With

                 Catch ex As Exception
                     MessageBox.Show("Error: Saving Image Failed ->>" & ex.Message.ToString())

                 Finally
                     sfdpic.Dispose()
                 End Try

             Catch ex As Exception
                 Me.Cursor = Cursors.Default
                 MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
             End Try

         End Using
     End If
     reader.Close()
     Cursor = Cursors.Default
 End If
 Cursor = Cursors.Default

 Try




I cannot save multiple image for filtered datagridview list. How can i do it?
Any example please.

解决方案

I noticed two errors right away.
Unfortunately VB is not my primary language and I do not have access to an IDE at the moment. You are going to need to work out any errors in syntax on your own.

Problem #1: Don't ever concatenate an SQL command together.
Use Parameterized queries to remove chances of SQL Injection
Fix: Change the WHERE clause

' BAD:
     "...where BADGE.ID Like '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "%'"

' Good:
     "...where BADGE.ID = @BadgeID"
     cmd.Parameters.AddWithValue("@BadgeID", DataGridView1.CurrentRow.Cells(0).Value);



Problem #2: Only 1 value retrieved
Fix: Loop through the reader

If reader.HasRows = False Then
	MessageBox.Show("Error Message")
Else
   While reader.Read()
      ' do your code here for each row in the reader object
   End While
End If


这篇关于如何通过VB net将多个图像(blob)从SQL表保存到图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆