我的代码问题是我可以正确插入数据库,但如果它会更新,则连接超时。因此,为了澄清我的问题,我的INSERT代码可以正常工作,但UPDATE并没有。 我已经使用过sql事务,因此数据仍然被锁定,我无法更新。
这是我的代码:
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 transaction As SqlTransaction
transaction = con.BeginTransaction("MyTransaction")
cmd.Transaction = transaction
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 ('" & Session.Item("HospNum") & "','" & Session.Item("IDNum") & "', '" & Session.Item("DoctorID") & "', @Data, '" & lblHeader.Text & "', 'PatientPhoto' , '" & FileSizeOfIMG.ToString & "', GETDATE())"
End If
reader.Close()
cmd.CommandText = SQLStr
cmd.Parameters.Add(ImageTypeDataOfImage)
transaction.Commit()
cmd.ExecuteNonQuery()
con.Close()
GetData()
End Using
End Sub