Can't UPDATE a large image size that was INSER

2019-07-31 23:02发布

Here is the problem, when I insert an image (let's call it Data A) which is 1.32MB, it will be inserted successfully. But if I will insert again Data A(but it will update now because i used UPSERT, see my code), it will not be updated and it will result to connection time out.

But when i insert another data (Data B) which is only 4KB, it will also be inserted successfully and if I will insert again into it(which is update), it will be updated successfully. What can I do? I cannot understand the problem. I already made my command timeout for 2 mins but nothing happened and it just loaded forever. I also used sql transaction but it did nothing.

Here is my code:

 Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim strConnString As String = DataSource.ConnectionString
    Using con As New SqlConnection(strConnString)
        Dim SQLStr As String

        Dim base64String = TextArea1.Value
        Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
        Dim FileSizeOfIMG As String
        FileSizeOfIMG = imageBytes.Length
        Dim ImageTypeDataOfImage As New SqlParameter("@Data", SqlDbType.Image)
        ImageTypeDataOfImage.Value = imageBytes

        SQLStr = "SELECT 1 FROM [Patient_Data].[dbo].[tbPatientImage]   where HospNum='" & Session.Item("HospNum") & "'" & _
                " and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "'"

        Dim cmd As New SqlCommand(SQLStr, con)
        cmd.Connection = con
        con.Open()

        Dim reader As SqlDataReader = cmd.ExecuteReader()

        If reader.Read() Then
            SQLStr = "UPDATE [Patient_Data].[dbo].[tbPatientImage] SET PatImage= @Data, FileSize= '" & FileSizeOfIMG.ToString & "' , TransDate = GetDate() where HospNum='" & Session.Item("HospNum") & "' and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "'"
        Else
            SQLStr = "INSERT INTO [Patient_Data].[dbo].[tbPatientImage](HospNum,IDNum, DoctorID, PatImage , FileType, FileName, FileSize , TransDATE) " & _
                    " VALUES (@HospNum,@IDNum, @DoctorID, @Data, @FileType, 'Patient Photo' , @FileSize,  GETDATE())"
        End If
        reader.Close()
        cmd.CommandText = SQLStr    
        cmd.Parameters.AddWithValue("@HospNum", Session.Item("HospNum"))
        cmd.Parameters.AddWithValue("@IDNum", Session.Item("IDNum"))
        cmd.Parameters.AddWithValue("@DoctorID", Session.Item("DoctorID"))
        cmd.Parameters.AddWithValue("@FileType", lblHeader.Text)
        cmd.Parameters.AddWithValue("@FileSize", FileSizeOfIMG.ToString)
        cmd.Parameters.Add(ImageTypeDataOfImage)
        cmd.ExecuteNonQuery()
        con.Close()
        GetData()

    End Using
End Sub

1条回答
做个烂人
2楼-- · 2019-07-31 23:30

I haven't figured out what causes this but i have figured out a remedy by having a delete query first then insert data.

   SQLStr = "delete FROM [Patient_Data].[dbo].[tbPatientImage]  where HospNum='" & Session.Item("HospNum") & "'" & _
            " and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "'"

        Dim cmd As New SqlCommand(SQLStr, con)
        cmd.Connection = con
        con.Open()

        cmd.ExecuteNonQuery()

        SQLStr = "  INSERT INTO [Patient_Data].[dbo].[tbPatientImage](HospNum,IDNum, DoctorID, PatImage , FileType, FileName, FileSize , TransDATE) " & _
                " VALUES (@HospNum,@IDNum, @DoctorID, @Data, @FileType, 'Patient Photo' , @FileSize,  GETDATE())"

        cmd.CommandText = SQLStr
        'cmd.CommandTimeout = 120     
        cmd.Parameters.AddWithValue("@HospNum", Session.Item("HospNum"))
        cmd.Parameters.AddWithValue("@IDNum", Session.Item("IDNum"))
        cmd.Parameters.AddWithValue("@DoctorID", Session.Item("DoctorID"))
        cmd.Parameters.AddWithValue("@FileType", lblHeader.Text)
        cmd.Parameters.AddWithValue("@FileSize", FileSizeOfIMG)
        cmd.Parameters.Add(ImageTypeDataOfImage)
        cmd.ExecuteNonQuery()
        con.Close()
        GetData()
        lblMessage.Text = "Saved."

    End Using
End Sub
查看更多
登录 后发表回答