我已经编写了下面的代码来删除多个数据库表中的记录,但是一些代码如何不删除任何记录并且也不显示任何错误!有人可以帮忙吗?
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
Try
cmd = New SqlCommand("IF EXISTS(SELECT * FROM book WHERE isbn = @isbn) " _
& " BEGIN " _
& " delete from published_by where isbn = @isbn; " _
& " delete from return where isbn = @isbn; " _
& " delete from issue where isbn = @isbn; " _
& " delete from book where isbn = @isbn;" _
& " SELECT 1; " _
& " END " _
& " ELSE SELECT 0", cn)
cmd.Parameters.Add(New SqlParameter("@isbn", SqlDbType.VarChar, 13) _
With {.Value = isbn})
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
Dim returnValue As Integer = CInt(cmd.ExecuteScalar())
If returnValue = 1 Then
lblAlert.Text = ""
lblInfo.Text = "Deleted"
Else
lblAlert.Text = "No Record Available"
lblInfo.Text = ""
End If
Catch sqlEx As SqlException
Select sqlEx.Number
Case -1, 2, 53
MessageBox.Show("Connection Failed to Database")
End Select
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
答案 0 :(得分:2)
"返回"是一个保留字(见this reference)。要解决此问题,请将其放在如下所示的方括号中:
delete from [return] where isbn ...
关于您的代码的另一个注意事项是,如果错误编号不是-1,2或53,您正在吞咽SqlException
。要解决此问题,请处理不是这些数字之一的情况这样:
Select Case sqlEx.Number
Case -1, 2, 53
Console.WriteLine("Connection Failed to Database")
Case Else
Console.WriteLine(....) 'write something here, you can use sqlEx.Message
End Select
这就是您首先没有收到错误消息的原因。