如何在VB网络中将多个blob图像从SQL保存到文件(再次)? [英] How to save multiple blob image from SQL to file (once again) in VB net?

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

问题描述

大家好,



我的代码有第二次改动,但仍然有问题 - 它只保存了一个文件并停止了。



如何更改我的代码以保存来自fltered datagrid的所有照片?



非常感谢任何建议和帮助。



我尝试了什么:



此代码已加载数据从sql到DataGridView:



 OpenConnection()'在

之前打开我们的连接Dim da As New SqlDataAdapter
Dim dt As DataTable'声明数据表
Dim bs As New BindingSource()

da = New SqlDataAdapter(选择emp.SSNO为'Number',emp.LASTNAME为'LASTNAME ',emp.FIRSTNAME作为'FIRSTNAME',emp.MIDNAME作为'MIDNAME',MMOBJS.LNL_BLOB作为'照片'来自EMP INMP JOIN BADGE EMP.ID = BADGE.EMPID INNER JOIN UDFEMP ON UDFEMP.ID = EMP.ID INNER在UDFEMP.DEPT = DEPT.ID INNER JOIN BADGSTAT B上加入DEPT ADGE.STATUS = BADGSTAT.ID INNER JOIN MMOBJS ON MMOBJS.EMPID = BADGE.EMPID,connections)
dt = New DataTable

da.Fill(dt)
bs = New BindingSource()
bs.DataSource = dt
DataGridView1.DataSource = bs
BindingNavigator1.BindingSource = bs

Dim band As DataGridViewBand = DataGridView1.Columns(4)
band.Visible = False
connections.Close()'close connections

End Sub





然后,通过点击保存按钮,我想保存到本地文件夹所有用户照片来自过滤的DataGrid,每张照片的原始文件名。但是我的代码只从网格中保存了一张照片(看到的行),那就是全部(((



 Private Sub Button1_Click(sender As Object,e作为EventArgs)处理Button1.Click 

Dim Path As String = IO.Path.Combine(Application.StartupPath,Images,
+ DataGridView1.CurrentRow.Cells(0)。 Value.ToString +
+ DataGridView1.CurrentRow.Cells(1).Value.ToString +
+ DataGridView1.CurrentRow.Cells(2).Value.ToString +
+ DataGridView1.CurrentRow.Cells(3).Value.ToString +
+.jpg)
Dim Dir As String = System.IO.Path.GetDirectoryName(Path)


尝试
如果不是System.IO.Directory.Exists(Dir)那么
System.IO.Directory.CreateDirectory(Dir)
结束如果

Catch ex As Ex ception
MessageBox.Show(错误:保存图像失败 - >>& ex.Message.ToString())
结束尝试

SqlBlob2File(Path)

结束子


私人子SqlBlob2File(ByVal DestFilePath As String)

OpenConnection()'在
之前打开我们的连接Dim PictureCol As Integer = 4'BLOB字段的列#

' Dim cn As New SqlConnection(server = localhost; integrated security = yes; database = NorthWind)
Dim cmd As New SqlCommand(选择emp.SSNO为'Number',emp.LASTNAME为'LASTNAME',emp .FIRSTNAME作为'FIRSTNAME',emp.MIDNAME作为'MIDNAME',MMOBJS.LNL_BLOB作为'照片'来自EMP INNER JOIN BADGE 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 EMP.SSNO = @SSNO,connections)
cmd.Parameters.AddWithValue( @SSNO,DataGridView1.CurrentRow.Cells(0).Value)

Dim dr As SqlDataRea der = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
'dr.Read()
if dr.HasRows Then

while dr.Read

Dim b(dr.GetBytes(PictureCol,0,Nothing,0,Integer.MaxValue) - 1)As Byte
dr.GetBytes(PictureCol,0,b,0,b.Length)


Dim fs As New System.IO.FileStream(DestFilePath,IO.FileMode.OpenOrCreate,IO.FileAccess.Write)
fs.Write(b,0,b.Length)
fs .Close()
End
End if
dr.Close()
connections.Close()'close connections

End Sub

解决方案

使用此方法浏览datagridview中的列





< pre>  Dim  x  As  DataGridViewRow =  DataGridViewRow( )
对于 每个 x DataGridView1.Rows
' 修改此值
Dim 路径作为 字符串 = IO.Path.Combine(Application.StartupPath , 图像
+ x.Cells( 0 )。Value.ToString +
+ x.Cells( 1 )。值.ToString +
+ x.Cells( 2 )。Value.ToString +
+ x.Cells( 3 )。Value.ToString +
+ 。jpg
' 和其他
cmd.Parameters.AddWithValue( @ SSNO,x.Cells( 0 )。值)

下一步


Hello everybody,

I have a second itteration of my code, and still have a problem - it's only one file saved and stoped.

How i need to change my code for saving all photos from fltered datagrid?

Thanks a lot for any advise and help.

What I have tried:

This code is loaded data from sql to DataGridView:

OpenConnection() ' open our connection before

    Dim da As New SqlDataAdapter
    Dim dt As DataTable ' declaration data table
    Dim bs As New BindingSource()

    da = New SqlDataAdapter("Select emp.SSNO As 'Number', emp.LASTNAME As 'LASTNAME', emp.FIRSTNAME As 'FIRSTNAME', emp.MIDNAME As 'MIDNAME', MMOBJS.LNL_BLOB As 'Photo' 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", connections)
    dt = New DataTable

    da.Fill(dt)
    bs = New BindingSource()
    bs.DataSource = dt
    DataGridView1.DataSource = bs
    BindingNavigator1.BindingSource = bs

    Dim band As DataGridViewBand = DataGridView1.Columns(4)
    band.Visible = False
    connections.Close() ' close connections

End Sub



Then, by click on "Save" button, i want to save to local folder all users photo from filtered DataGrid, with original filename for each photo. But my code saved only one photo from grid (seected row) and thats all (((

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim Path As String = IO.Path.Combine(Application.StartupPath, "Images",
                                         " " + DataGridView1.CurrentRow.Cells(0).Value.ToString +
                                         " " + DataGridView1.CurrentRow.Cells(1).Value.ToString +
                                         " " + DataGridView1.CurrentRow.Cells(2).Value.ToString +
                                         " " + DataGridView1.CurrentRow.Cells(3).Value.ToString +
                                         "" + ".jpg")
    Dim Dir As String = System.IO.Path.GetDirectoryName(Path)


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

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

    SqlBlob2File(Path)
    
End Sub


Private Sub SqlBlob2File(ByVal DestFilePath As String)

    OpenConnection() ' open our connection before
    Dim PictureCol As Integer = 4 ' the column # of the BLOB field

    'Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
    Dim cmd As New SqlCommand("Select emp.SSNO As 'Number', emp.LASTNAME As 'LASTNAME', emp.FIRSTNAME As 'FIRSTNAME', emp.MIDNAME As 'MIDNAME', MMOBJS.LNL_BLOB As 'Photo' 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 EMP.SSNO = @SSNO", connections)
    cmd.Parameters.AddWithValue("@SSNO", DataGridView1.CurrentRow.Cells(0).Value)

    Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
    'dr.Read()
    If dr.HasRows Then

        While dr.Read

            Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
            dr.GetBytes(PictureCol, 0, b, 0, b.Length)


            Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
            fs.Write(b, 0, b.Length)
            fs.Close()
        End While
    End If
    dr.Close()
    connections.Close() ' close connections

End Sub

解决方案

Use this method to go through the columns in your datagridview


<pre> Dim x As DataGridViewRow = New DataGridViewRow()
                For Each x In DataGridView1.Rows
'modify this par
Dim Path As String = IO.Path.Combine(Application.StartupPath, "Images",
                                         " " + x.Cells(0).Value.ToString +
                                         " " + x.Cells(1).Value.ToString +
                                         " " + x.Cells(2).Value.ToString +
                                         " " + x.Cells(3).Value.ToString +
                                         "" + ".jpg")
'and this other
cmd.Parameters.AddWithValue("@SSNO", x.Cells(0).Value)

Next


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

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