Skip to content Skip to sidebar Skip to footer

How To Retrieve Varbinary Values From Sql Server 2008 Using Vb.net

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 100 000 odd rows. I just don't

Solution 1:

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 AsString
            qry = String.Format("SELECT field FROM test.dbo.test")
            cmd.CommandText = qry
            cmd.CommandTimeout = 0Dim 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 AsByte() = myReader(0)
                    oFileStream.Write(data, 0, data.Length)
                EndWhile
                oFileStream.Close()
            EndUsingEndUsingEndUsing

UPDATE: here is: another example on VB.NET

Solution 2:

this is other option and work fine

dim path asstring = "c:\myFile.pdf"Dim data AsByte() = TB.Rows(0)("documento")
Dim f As System.IO.File
f.WriteAllBytes(path, data)

Post a Comment for "How To Retrieve Varbinary Values From Sql Server 2008 Using Vb.net"