2014-12-13 30 views
0

嗨,大家好,我試圖顯示消息,如果數據庫中存在相同的Id。但如果語句不適用於OleDb.OleDbDataAdapte 我該如何解決這個問題並使If語句有效?如何避免在vb中添加兩次

If Da1 = New OleDb.OleDbDataAdapter("select * from Payments where BookingID = " & 
     CInt(txtBookingID.Text) & "", Con1) Then 

    MessageBox.Show("This Bill for this Booking is existed in the system. 
    You Can not add it again.", "Authentication Failure", MessageBoxButtons.OK, 
    MessageBoxIcon.Exclamation) 

else 

    new_rec("BookingID") = txtBookingID.Text 
    new_rec("DateAndTime") = lblDateAndTime.Text 

    Dt1.Rows.Add(new_rec) 
    Da1.Update(Dt1) 

    MessageBox.Show("Booking ID " + " *" + txtBookingID.Text + "* " + 
    "has been Saved to The System.  Press * OK * to Go Back") 

end if 
+0

此代碼使用的技術是容易受到SQL注入式攻擊。如果在應用程序的其他地方使用相同的技術,實際上乞求被黑客入侵。 – 2014-12-13 04:26:07

+0

另外:**你從不執行查詢**。只是創建一個數據適配器不會運行sql命令。你必須**填充()**數據集或數據表。 – 2014-12-13 04:27:26

+0

可能的重複[如何避免添加或插入信息兩次以訪問Visual Basic中的數據庫?](http://stackoverflow.com/questions/27447985/how-to-avoid-adding-or-inserting-information-twice-以訪問數據庫在視覺) – Plutonix 2014-12-13 12:53:54

回答

0

嘗試這樣

Da1 = New OleDb.OleDbDataAdapter("select * from Payments", Con1) 

da1.Fill(dt1) 

If dt1 is nothing orelse dt1.select("boolingid=" & txtBookingID.Text).count > 0 Then 

    MessageBox.Show("This Bill for this Booking is existed in the system. You 
    Can not add it again.", "Authentication Failure", MessageBoxButtons.OK, 
    MessageBoxIcon.Exclamation) 

else 

    new_rec("BookingID") = txtBookingID.Text 
    new_rec("DateAndTime") = lblDateAndTime.Text 

    Dt1.Rows.Add(new_rec) 
    Da1.Update(Dt1) 

    MessageBox.Show("Booking ID " + " *" + txtBookingID.Text + "* " + 
    "has been Saved to The System. Press * OK * to Go Back") 
end if 
+0

謝謝你的回覆。但這不會允許添加。 IT總是這樣做,如果不去其他語句 – 2014-12-13 06:27:46

+0

@EssaAlatwan不清楚用示例顯示。如果你在桌上有這個ID,那麼顯示警告,否則存儲。這是對的啊? – Sathish 2014-12-13 07:38:08

+0

是的,我想要什麼。如果表中存在Id,則顯示警告,否則應將信息存儲在新行中。 – 2014-12-13 18:57:14

相關問題