Im trying to insert binary data into a varbinary field in sql 2005 from
vb.net 2003.
Converting my data to binary I have finally gotten to work but now
inserting into tha database gives me a problem.
my code for binary conversion is as follows
Dim str As String
Dim chr As Char
str = ""
Dim reader As StreamReader
reader = New StreamReader("c:\test.txt")
While Not reader.EndOfStream
chr = ChrW(reader.Read)
str = str + cls.Cal_CharToBinary(chr)
End While
TextBox1.Text = str
with cls being:
Public Class CalculateFunctions
Public Function Cal_CharToBinary(ByVal CharStr As String) As String
Dim LastValue As Integer
Dim BinaryArr() As String
Dim BinaryStr As String
Dim i As Integer
LastValue = Asc(CharStr)
i = 0
ReDim BinaryArr(i)
While LastValue <> 0
ReDim Preserve BinaryArr(i)
BinaryArr(i) = LastValue Mod 2
LastValue = LastValue \ 2
i = i + 1
End While
If UBound(BinaryArr) >= 0 Then
For i = 0 To UBound(BinaryArr)
BinaryStr = BinaryArr(i) & BinaryStr
Next
BinaryStr = String.Format("0", 8 - Len(BinaryStr)) &
BinaryStr
End If
Cal_CharToBinary = BinaryStr
End Function
This all works fine, but when i try insert the converted binary data to
the database I get the following error :
The number that starts with
'01100001011000100110001101100100011001010110011001100111011010000110100101101010011010110110110001101101011011100110111101110000'
is too long. Maximum length is 128.
my code for doing this is
Dim myCn As New SqlConnection()
myCn.ConnectionString = "Data Source=sms;Initial
Catalog=Pictures;Persist Security Info=True;User
ID=sa;Password=38tx11local"
Dim cmd As New SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = ("INSERT INTO ImageLibrary (ImageBLOB) VALUES
(" + str + ")")
cmd.Connection = myCn
Dim rowCount As Integer
Dim previousConnectionState As ConnectionState
previousConnectionState = myCn.State
Try
If myCn.State = ConnectionState.Closed Then
myCn.Open()
End If
rowCount = cmd.ExecuteNonQuery()
Finally
If previousConnectionState = ConnectionState.Closed Then
myCn.Close()
End If
End Try
please help if you can........
Fasan