How to retrieve VARBINARY values from SQL Server 2008 using VB.Net

前端 未结 2 379
轻奢々
轻奢々 2021-01-26 05:02

I\'m trying to populate a listview with varBinary(max) values. Well, I actually need to write each varBinary into a csv file and the table consists of

相关标签:
2条回答
  • 2021-01-26 05:15
        Using cn As SqlConnection = New SqlConnection("Server=.;Database=test;Trusted_Connection=True;")
            cn.Open()
            Using cmd As SqlCommand = New SqlCommand()
                cmd.Connection = cn
                Dim qry As String
                qry = String.Format("SELECT field FROM test.dbo.test")
                cmd.CommandText = qry
                cmd.CommandTimeout = 0
                Dim oFileStream As System.IO.FileStream
                oFileStream = New System.IO.FileStream("c:\bytes.txt", System.IO.FileMode.Append)
                Using myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                    While (myReader.Read())
                        Dim data As Byte() = myReader(0)
                        oFileStream.Write(data, 0, data.Length)
                    End While
                    oFileStream.Close()
                End Using
            End Using
        End Using
    

    UPDATE: here is: another example on VB.NET

    0 讨论(0)
  • 2021-01-26 05:35

    this is other option and work fine

    dim path as string = "c:\myFile.pdf"
    Dim data As Byte() = TB.Rows(0)("documento")
    Dim f As System.IO.File
    f.WriteAllBytes(path, data)
    
    0 讨论(0)
提交回复
热议问题