2015-11-23 31 views
1

我已經編寫代碼在VB中使用inputbox(通過用戶選擇)從數據庫中刪除表記錄,但我有一個問題,當用戶插入錯誤值仍然顯示「記錄已成功刪除」MessageBox!如果在Visual Basic中的其他條件刪除記錄從SQL Server如果記錄存在

如何設置「if條件」來顯示記錄不存在,而不是顯示成功的刪除消息?

對不起,這是我的第一篇文章,這就是爲什麼它很長! :P

下面是代碼:

Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click 
     Try 
      Dim isbn As Long = InputBox("Enter Book ISBN", "Delete") 
      'First will delete the dependant record from published_by, book_return, memberbook_issue because 
      'it can not be deleted by applying on cascade delete or update cause it has composite primary key. 

      cmd = New SqlCommand("delete from published_by where isbn =" & isbn, cn) 
      If cn.State = ConnectionState.Closed Then 
       cn.Open() 
      End If 
      cmd.ExecuteNonQuery() 

      cmd = New SqlCommand("delete from book_return where isbn =" & isbn, cn) 
      If cn.State = ConnectionState.Closed Then 
       cn.Open() 
      End If 
      cmd.ExecuteNonQuery() 

      cmd = New SqlCommand("delete from memberbook_issue where isbn =" & isbn, cn) 
      If cn.State = ConnectionState.Closed Then 
       cn.Open() 
      End If 
      cmd.ExecuteNonQuery() 


      cmd = New SqlCommand("delete from book where isbn = " & isbn, cn) 
      If cn.State = ConnectionState.Closed Then 
       cn.Open() 
      End If 
      cmd.ExecuteNonQuery() 
      da = New SqlDataAdapter("select b.staff_id, b.pub_id, b.sub_code, b.isbn, b.book_name, b.author, b.price, b.rack_no, b.no_of_books, pby.vol_no, pby.pub_date from book b join published_by pby on b.isbn = pby.isbn", cn) 
      dt = New DataTable 
      da.Fill(dt) 
      dgvbook.DataSource = dt 
      MessageBox.Show("Record Successfully Deleted from current table & dependant table(s)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
     Catch ex As Exception 
      MessageBox.Show("Not Completed Because OF The Following Error " & "%" & ex.Message & "%", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     End Try 
    End Sub 
+1

'......這就是爲什麼它很長'不夠長。請參閱[問] – Plutonix

+0

如果您發佈您的代碼,我們將能夠爲您提供幫助 – Dane

+0

我可以將它發佈在評論部分嗎? – Hazmat

回答

2

正如其他人已經提到的,你真的需要考慮參數化查詢。您還應該將其封裝到存儲過程中,以便讀取和維護。除此之外,您可以在一次數據庫調用中完成所有這些操作。

這段代碼應該會產生預期的結果。請仔細檢查SqlParameter上的數據類型 - 我猜想它可能是什麼。

cmd = New SqlCommand("IF EXISTS(SELECT * FROM book WHERE isbn = @isbn) " _ 
    & " BEGIN " _ 
    & " delete from published_by where isbn = @isbn; " _ 
    & " delete from book_return where isbn = @isbn; " _ 
    & " delete from memberbook_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, 20) With {.Value = isbn}) 

cn.Open() 
Dim returnValue as Integer = CInt(cmd.ExecuteScalar()) 
If returnValue = 1 Then 
    MessageBox.Show("Record Successfully Deleted from current table & dependant table(s)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
End 
+0

非常感謝@InbetweenWeekends我瞭解您與我分享的代碼的每一部分,並且它正在工作。只有一件事我在SQL中使用了「numeric(13,0)」,在VB.NET中使用了Integer作爲ISBN!現在應該在代碼中使用哪種數據類型而不是varchar來與我在代碼中提供的數據類型匹配? – Hazmat

+2

@Hazmat如果你還沒有查看它,那麼爲了將來人們看到這一點,「具有{.ParameterName =」@isbn「,.SqlDbType = SqlDbType.Decimal,.Precision = 13的新SqlParameter, .Scale = 0,.Value = isbn}'。 –

+0

@Hazmat - 你的答案可以在Andrew的評論中找到。 – InbetweenWeekends

相關問題