2011-10-22 61 views
1

我有一個'invoices'表和發票號碼列。檢查表中是否存在值;顯示消息框

添加新發票時,我想檢查它是否已經存在。如果存在,則向用戶生成一個MessageBox,表示已經有該號碼的記錄。

這怎麼辦?

我使用Visual Basic 2010和SQL Server 2008

回答

3

嘗試是這樣的:

Dim SomeInvoiceNumber As String = "" 
Using conn As New SqlClient.SqlConnection("YourConnectionString") 
    Using cmd As SqlClient.SqlCommand = conn.CreateCommand() 

     cmd.CommandText = "SELECT 1 FROM Invoices WHERE InvoiceNumber = @InvNum" 
     cmd.Parameters.AddWithValue("@InvNum", SomeInvoiceNumber) 
     conn.Open() 
     Dim exists As String = cmd.ExecuteScalar().ToString() 

     If exists = "1" Then 
      MessageBox.Show(SomeInvoiceNumber & " exists already.") 
     End If 
    End Using 
End Using 
相關問題