基于这个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
我试过多种方式让它显示,但没有任何工作到目前为止。