如何使用VB.Net从SQL Server 2008中检索VARBINARY值。

问题描述 投票:0回答:2

我想在一个列表视图中填入 varBinary(max) 值。好吧,其实我需要把每个 varBinary 到csv文件中,而该表由100 000条奇数行组成。

我只是不知道如何检索数据类型!

    Sub getInformation()
    Try
        If Not String.IsNullOrWhiteSpace(connString) Then
            Using cn As SqlConnection = New SqlConnection(connString)
                cn.Open()
                Using cmd As SqlCommand = New SqlCommand()
                    cmd.Connection = cn
                    Dim qry As String
                    qry = String.Format("SELECT [varBinaryField] FROM [dbX].[dbo].[tableX]")
                    cmd.CommandText = qry
                    cmd.CommandTimeout = 0

                    Using myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                        While (myReader.Read())
                            ListView1.Items.Add(myReader.get?WHAT?(0)) 'Help needed here.
                        End While
                    End Using
                End Using
            End Using
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

下面是一个varBinary代码的例子。

sql-server-2008 vb.net-2010 sqldatatypes
2个回答
2
投票
    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

更新。 这里是: 另一个例子在VB.NET上


0
投票

这是另一种选择,工作正常

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)
© www.soinside.com 2019 - 2024. All rights reserved.