Sql 如何将数据插入VB.net中的两个不同表中,我使用MS Access作为我的数据库
我在使用VS2010时遇到问题。我正在使用MS Access数据库 我有三张桌子:TBL客户、TBL预订、TBL房间 TBL客户有以下列:Sql 如何将数据插入VB.net中的两个不同表中,我使用MS Access作为我的数据库,sql,vb.net,ms-access,Sql,Vb.net,Ms Access,我在使用VS2010时遇到问题。我正在使用MS Access数据库 我有三张桌子:TBL客户、TBL预订、TBL房间 TBL客户有以下列: (1) Customer_ID (2) Last_Name (3) First_Name (4) Age (5) Address (6) Contact (1) Book_No (2) Customer_ID (3) Day_In (4) Day_Out (5) Room_ID (1) Room_ID (2) Room_Type (3) Price (4
(1) Customer_ID
(2) Last_Name
(3) First_Name
(4) Age
(5) Address
(6) Contact
(1) Book_No
(2) Customer_ID
(3) Day_In
(4) Day_Out
(5) Room_ID
(1) Room_ID
(2) Room_Type
(3) Price
(4) Capacity
(5) Remarks
tblBooking有以下列:
(1) Customer_ID
(2) Last_Name
(3) First_Name
(4) Age
(5) Address
(6) Contact
(1) Book_No
(2) Customer_ID
(3) Day_In
(4) Day_Out
(5) Room_ID
(1) Room_ID
(2) Room_Type
(3) Price
(4) Capacity
(5) Remarks
tblRoom有以下列:
(1) Customer_ID
(2) Last_Name
(3) First_Name
(4) Age
(5) Address
(6) Contact
(1) Book_No
(2) Customer_ID
(3) Day_In
(4) Day_Out
(5) Room_ID
(1) Room_ID
(2) Room_Type
(3) Price
(4) Capacity
(5) Remarks
现在,每当我输入lname、fname、年龄、地址、联系人时,选择房间类型、容量,输入和输出首选日期,然后单击“立即预订”!它说这是成功的。但当我查看数据库时,只有tblCustomers表被完全填写
有人能给我提些建议吗?这样我就可以填写tblBooking表格了
我也试过加入他们,但没用。提前感谢您的回复
这是我编写的代码。问题是,它在tblCustomers表中双重插入信息
Private Sub bookBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bookBtn.Click
Try
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = HotelRsvp.accdb"
query = "INSERT INTO tblCustomers (Last_Name, First_Name, Age, Address, Contact) VALUES ('" & lnametxtbx.Text.ToLower() & "', '" & fnametxtbx.Text.ToLower() & "', '" & agetxtbx.Text & "', '" & addtxtbx.Text.ToLower() & "', '" & contacttxtbx.Text & "')"
query2 = "INSERT INTO tblBooking WHERE Customer_ID = tblCustomer.Customer_ID, Day_In = '" & dayIn.Value & "', Day_Out = '" & dayOut.Value & "', Room_ID = '" & rmIDlbl.Text & "'"
dbUp = New OleDbCommand(query, con)
dbUp2 = New OleDbCommand(query2, con)
con.Open()
dbUp.ExecuteNonQuery()
dbUp2.ExecuteNonQuery()
MessageBox.Show("Successfully Booked.")
lnametxtbx.Text = ""
fnametxtbx.Text = ""
agetxtbx.Text = ""
addtxtbx.Text = ""
contacttxtbx.Text = ""
RmtypeCbx.ResetText()
cpctyCbx.ResetText()
rmIDlbl.Text = ""
Pricelbl.Text = ""
con.Close()
Catch ex As Exception
If Not IsNumeric(contacttxtbx.Text) Or Not IsNumeric(agetxtbx.Text) Then
MessageBox.Show("Invalid Age, Contact Number or there's a blank.")
Else
'con.Open()
dbUp = New OleDbCommand(query, con)
dbUp.ExecuteNonQuery()
MessageBox.Show("Successfully Booked.")
con.Close()
End If
End Try
con.Close()
End Sub
查询2未指定值:
query2 = "INSERT INTO tblBooking WHERE Customer_ID = tblCustomer.Customer_ID, Day_In = '" & dayIn.Value & "', Day_Out = '" & dayOut.Value & "', Room_ID = '" & rmIDlbl.Text & "'"
就像你在问题1中所做的那样。您正在编写where子句,但没有编写值
query = "INSERT INTO tblCustomers (Last_Name, First_Name, Age, Address, Contact) VALUES ('" & lnametxtbx.Text.ToLower() & "', '" & fnametxtbx.Text.ToLower() & "', '" & agetxtbx.Text & "', '" & addtxtbx.Text.ToLower() & "', '" & contacttxtbx.Text & "')"
应该是:
query2 =
"INSERT INTO tblBooking (COL1,COL2,COL3) VALUES (VAL1,VAL2,VAL3) WHERE Customer_ID = tblCustomer.Customer_ID, Day_In = '" & dayIn.Value & "', Day_Out = '" & dayOut.Value & "', Room_ID = '" & rmIDlbl.Text & "'"
请粘贴源代码好的,请稍候。我会重新编码。@CarlosLanderas,有源代码bro.query2=插入tblBooking Customer\u ID、Day\u in、Day\u Out、Room\u ID值这是问题所在,我无法从tblCustomers中选择要插入tblBooking客户ID的客户ID。您的意思是您没有该ID。您应该查询数据库以恢复已插入数据库的客户ID。我的想法是从tblCustomers中获取客户ID,然后在tblBooking中也使用它,因为它们是相同的。PK和FK。这正是我说的。从插入的客户处恢复ID并在预订表中使用;那么,我将使用SELECT语句?