2013-07-12 57 views
0

我試圖用varBinary(max)值填充列表視圖。那麼,我實際上需要將每個varBinary寫入一個csv文件,該表由100 000個奇數行組成。如何使用VB.Net從SQL Server 2008檢索VARBINARY值

我只是不知道如何檢索數據類型!

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代碼之一的一個示例:

0x00100800C40400000210080004000000000400020810080004000000020306030B10080004000000020306030910080004000000000604040510080004000000A00100000610080004000000A001000007100800040000000000FA430C10080004000000000000000A100800040000000000000000110800500400000111080004000000010000000211080004000000000000000311080004000000010200000411080004000000040000001011080004000000010000002011080004000000010000001311080004000000A00100001411080004000000A00100001111080004000000D00000001211080004000000D00000001511080004000000000000001611080004000000010000000611080004000000FF000 0002311080004000000A00100002411080004000000A00100002111080004000000D00000002211080004000000D0000000001208007C030000101208000400000002000000111208000400000000000000261208000400000001000000201208000400000043000000211208002403000042012D00DD7E3C400D390000C5003B00000000000D39000007013B00DD7E3C400D390000470146001CA3BF400D39000021014B00DD7E3C400D3900006E014B0060A529400D390000FE0059001CA3BF400D390000590173005E3636400D390000F2009400D8A055400D3900008F0194009E5AB9400D3900006501A200DFED2F400D3900001801AB005CC742400D3900005501AE00DD7EBC400D390000DF00B700E4CB963E0D3900000701BA0059584F400D3900004B01BE00DD7E3C400D3900006C01CC00DFED2F400D390000BA00CF00DB0F493F0D3900002101D10059584F400D3900000901D30057E95B400D3900006E01E2009E5AB9400D390000E400F700D4C26E400D390000F2000001520B75400D39000023010001D4C26E400D39000055010001DD7EBC400D390000FE000201D1537B400D390000B3010501E4CB16400D390000A2000701D1537B3F0D3900008601070163141D400D3900001A011A0167F283400D3900002D012101A71687400D39000094012A01658310400D390000CA002 D01E63A8A400D390000CC013401D4C2EE3F0E39000031013801A238A0400E390000B5013801E63A0A400E3900004E013F0160A5A9400E3900001501490163149D400E390000A2014B01658310400E3900006C015001A0C9AC400E390000A2005201E25CA33F0E390000C1015C01E4CB16400E390000D1005E01E4CB96400E390000F000670163149D400E39000078016E01658310400E39000078017D015E36B6400E390000A701880160A529400E390000AB00A401A238A0400E3900006701A9019E5AB9400E3936000C01AE01A0C9AC400E393B00B501AE015CC742400E394200BE01B701DB0F49400E3949009401BA015E3636400E3973006501C3011CA3BF400E3975007101C30160A529400E397A003F01C801E4CB16400E397A002801D80163141D400E397A007801D801DFED2F400E397F000701DB01E4CB16400E398D00A701DB0159584F400E399D004201E40160A529400E39A2006301E401DFED2F400E39AC000701E901E25C23400E39AC00EB00EB01E25C23400E39B5005901F401DD7E3C400E39BA00D8000202DD7E3C400E39BC0063010C02DB0F49400E39CA000013080018000000311308000400000090000000351308000400000066660641

+0

的,什麼是這一堆二進制立場嗎?照片?一個文件? –

+0

就數據庫服務器而言,'VARBINARY'只是一堆字節,它可以是字面意義上的任何內容,而數據庫服務器不關心。您可以使用@sqladmin的建議來轉儲文件中的一些測試數據,並查看該文件以查看實際存儲在那裏的內容。 – fvu

+0

這是圖片.. –

回答

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 

UPDATE:這裏是:another example on VB.NET

+0

我想以字符串格式保存實際的二進制代碼本身。 –

+0

在一個文本文件中,最好是 –

+0

@DeanHart - 只需使用'StreamWriter'對象或'FileStream'對象將其寫入文件 – sqladmin

相關問題