基於這個VB代碼,我得到了一切在我的SQL數據庫中正確創建,而不是顯示在txtfoodid中的Food_ID它彈出在消息框中(我認爲這是因爲Try/Catch )。文本框顯示不正確
Dim con As New OleDbConnection(DBcon)
Try
Dim dr As OleDbDataReader
Dim command As New OleDbCommand("Insert into Donation (Donor_ID) VALUES (" & txtDonNum.Text & "); Select @@Identity;")
con.Open()
command.Connection = con
dr = command.ExecuteReader
Dim Donation_ID As String = ""
If dr.Read() Then
Donation_ID = dr(0).ToString
Dim food As New OleDbCommand("Insert into Food_Donation (Date_Received, Donation_ID) Values ('" & maskedreceived.Text & "', " & Donation_ID & "); Select @@Identity")
food.Connection = con
dr = food.ExecuteReader()
'food.ExecuteNonQuery()
End If
Dim Food_ID As String
If dr.Read() Then
Food_ID = dr(0).ToString
txtfoodid.Text = dr("Food_ID").ToString
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Close()
End Try
MessageBox.Show("Food_ID has been made.")
End Sub
我試過多種方式讓它顯示,但沒有任何工作到目前爲止。