如果存在,请更新。其他插入。连接超时错误

时间:2014-09-12 01:48:30

标签: sql asp.net vb.net insert

我的代码问题是我可以正确插入数据库,但是如果它会更新,那么 连接超时。因此,为了澄清我的问题,我的INSERT代码可以正常工作,但UPDATE没有。

这是我的代码:

  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)
        Using cmd As New SqlCommand()

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


            Dim SQLStr As String
            SQLStr = " IF EXISTS (SELECT * FROM [Patient_Data].[dbo].[tbPatientImage]   where HospNum='" & Session.Item("HospNum") & "' and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "')  " & _
                     " UPDATE [Patient_Data].[dbo].[tbPatientImage] SET PatImage= @Data where HospNum='" & Session.Item("HospNum") & "' and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "'  " & _
                     " else INSERT INTO [Patient_Data].[dbo].[tbPatientImage](HospNum,IDNum, DoctorID, PatImage , FileType, FileName, TransDATE) VALUES ('" & Session.Item("HospNum") & "','" & Session.Item("IDNum") & "', '" & Session.Item("DoctorID") & "', @Data, '" & lblHeader.Text & "', 'PatientPhoto' ,  GETDATE()) "

            cmd.CommandText = SQLStr
            cmd.Parameters.Add(ImageTypeDataOfImage)
            cmd.Connection = con
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()

        End Using
    End Using
End Sub

1 个答案:

答案 0 :(得分:1)

您好像不是COMMITROLLBACK您的交易。

这意味着,虽然您关闭了连接,但您之前的会话仍然保持对行的锁定,您尝试更新。因此,下次当收到同一记录的更新时,数据库会识别出有另一个会话对此记录进行锁定,因此除非它被释放,否则不能再对其执行更新或删除。 ,那会无限期地等待,导致你的超时。

然而,每个INSERT将是一个新条目,因此,前一个。行级锁定不会影响它们。在关闭连接之前,尝试在DBA的帮助下终止所有这些会话并在代码中包含COMMIT。

The Documentation here有一些示例来说明如何进行COMMITS反应。

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)
    Using cmd As New SqlCommand()
    Dim base64String = TextArea1.Value
    Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
    Dim ImageTypeDataOfImage As New SqlParameter("@Data", SqlDbType.Image)
    ImageTypeDataOfImage.Value = imageBytes

    Dim transaction As SqlTransaction

    transaction = con.BeginTransaction("MyTransaction");

    Dim SQLStr As String
    SQLStr = " IF EXISTS (SELECT * FROM [Patient_Data].[dbo].[tbPatientImage]   where HospNum='" & Session.Item("HospNum") & "' and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "')  " & _
             " UPDATE [Patient_Data].[dbo].[tbPatientImage] SET PatImage= @Data where HospNum='" & Session.Item("HospNum") & "' and IDNum='" & Session.Item("IDNum") & "' and FileType= '" & lblHeader.Text & "'  " & _
             " else INSERT INTO [Patient_Data].[dbo].[tbPatientImage]

(HospNum,IDNum, DoctorID, PatImage , FileType, FileName, TransDATE) VALUES ('" & Session.Item("HospNum") & "','" & Session.Item("IDNum") & "', '" & Session.Item("DoctorID") & "', @Data, '" & lblHeader.Text & "', 'PatientPhoto' ,  GETDATE()) "

            cmd.CommandText = SQLStr
            cmd.Parameters.Add(ImageTypeDataOfImage)
            cmd.Connection = con
            cmd.Transaction = transaction
            con.Open()
            cmd.ExecuteNonQuery()

            transaction.Commit()

            con.Close()

        End Using
    End Using
End Sub