Can't UPDATE a large image size that was INSERTed in the database in asp.net

谁说胖子不能爱 提交于 2020-01-16 04:59:15

问题


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:


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


来源:https://stackoverflow.com/questions/25866018/cant-update-a-large-image-size-that-was-inserted-in-the-database-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!