G
Guest
Hi Guy,
I've found that SqlDataReader.Read() (with do not read value from row) has a
problem when reading image data type from Ms SqlServer 2000 if data size is
65,535. I've found this error in .NET 1.1. Any suggestions?
Thanks,
The followings are sql script and VB.NET code:
----- sql script for table -----
CREATE TABLE [TB_TEST] (
[ID] [int] NOT NULL ,
[Data] [image] NULL ,
CONSTRAINT [PK_TB_Test] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
----- code ------
' insert test data
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim conn As New SqlConnection(connStr)
Dim cmd As SqlCommand
Dim qrt As String = "INSERT INTO TB_TEST ([ID], Data) VALUES (@id,
@data)"
Dim params(1) As SqlParameter
Dim data() As Byte
Try
conn.Open()
cmd = New SqlCommand(qrt)
cmd.Connection = conn
For i As Integer = 1 To 10
params(0) = New SqlParameter("@id", i)
Dim str As New String(ChrW(i + 65), 65535)
data = System.Text.Encoding.ASCII.GetBytes(str.ToCharArray())
params(1) = New SqlParameter("@data", data)
cmd.Parameters.Clear()
cmd.Parameters.Add(params(0))
cmd.Parameters.Add(params(1))
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
Console.WriteLine("Exception: " & ex.ToString())
Finally
If Not conn Is Nothing Then
conn.Close()
conn.Dispose()
conn = Nothing
End If
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
End Try
MsgBox("Success!")
End Sub
' retrieve data
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim conn As New SqlConnection(connStr)
Dim cmd As SqlCommand = Nothing
Dim qrt As String = "SELECT [ID], Data FROM TB_TEST"
Dim rdr As SqlDataReader = Nothing
Dim i As Integer = 0
Try
conn.Open()
cmd = New SqlCommand(qrt)
cmd.Connection = conn
rdr = cmd.ExecuteReader()
While Not rdr Is Nothing AndAlso rdr.Read() ' <--- Can read 1
record
i += 1
End While
Catch ex As Exception
Console.WriteLine("Exception: " & ex.ToString())
Finally
If Not conn Is Nothing Then
conn.Close() ' <--- InvalidOperationException
conn.Dispose()
conn = Nothing
End If
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
End Try
MsgBox("Success!")
End Sub
I've found that SqlDataReader.Read() (with do not read value from row) has a
problem when reading image data type from Ms SqlServer 2000 if data size is
65,535. I've found this error in .NET 1.1. Any suggestions?
Thanks,
The followings are sql script and VB.NET code:
----- sql script for table -----
CREATE TABLE [TB_TEST] (
[ID] [int] NOT NULL ,
[Data] [image] NULL ,
CONSTRAINT [PK_TB_Test] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
----- code ------
' insert test data
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim conn As New SqlConnection(connStr)
Dim cmd As SqlCommand
Dim qrt As String = "INSERT INTO TB_TEST ([ID], Data) VALUES (@id,
@data)"
Dim params(1) As SqlParameter
Dim data() As Byte
Try
conn.Open()
cmd = New SqlCommand(qrt)
cmd.Connection = conn
For i As Integer = 1 To 10
params(0) = New SqlParameter("@id", i)
Dim str As New String(ChrW(i + 65), 65535)
data = System.Text.Encoding.ASCII.GetBytes(str.ToCharArray())
params(1) = New SqlParameter("@data", data)
cmd.Parameters.Clear()
cmd.Parameters.Add(params(0))
cmd.Parameters.Add(params(1))
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
Console.WriteLine("Exception: " & ex.ToString())
Finally
If Not conn Is Nothing Then
conn.Close()
conn.Dispose()
conn = Nothing
End If
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
End Try
MsgBox("Success!")
End Sub
' retrieve data
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim conn As New SqlConnection(connStr)
Dim cmd As SqlCommand = Nothing
Dim qrt As String = "SELECT [ID], Data FROM TB_TEST"
Dim rdr As SqlDataReader = Nothing
Dim i As Integer = 0
Try
conn.Open()
cmd = New SqlCommand(qrt)
cmd.Connection = conn
rdr = cmd.ExecuteReader()
While Not rdr Is Nothing AndAlso rdr.Read() ' <--- Can read 1
record
i += 1
End While
Catch ex As Exception
Console.WriteLine("Exception: " & ex.ToString())
Finally
If Not conn Is Nothing Then
conn.Close() ' <--- InvalidOperationException
conn.Dispose()
conn = Nothing
End If
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
End Try
MsgBox("Success!")
End Sub