[英]How can I insert data into 2 different table in VB.net, I'm using MS Access as my db
I'm having a trouble here using VS2010. 我在使用VS2010时遇到了麻烦。 I'm using a MS Access database. 我正在使用MS Access数据库。
I have 3 tables: tblCustomers
, tblBooking
, tblRoom
我有3个表: tblCustomers
, tblBooking
, tblRoom
tblCustomers
has these columns: tblCustomers
具有以下列:
(1) Customer_ID
(2) Last_Name
(3) First_Name
(4) Age
(5) Address
(6) Contact
tblBooking
has these columns: tblBooking
具有以下列:
(1) Book_No
(2) Customer_ID
(3) Day_In
(4) Day_Out
(5) Room_ID
tblRoom
has these columns: tblRoom
具有以下列:
(1) Room_ID
(2) Room_Type
(3) Price
(4) Capacity
(5) Remarks
Now whenever I entered lname, fname, age, address, contact, choose a room type, capacity, put the preferred day_in and day_out and then click Book Now! 现在,每当我输入lname,fname,年龄,地址,联系方式,选择房间类型,可容纳人数时,输入首选的day_in和day_out,然后单击“立即预订”! It says it is successfully done. 它说成功完成了。 But when I look at my database, only the tblCustomers
table are completely filled in. 但是,当我查看数据库时,仅完整填写了tblCustomers
表。
Can anyone suggest something so that I can also fill in the tblBooking
table. 任何人都可以提出一些建议,以便我也可以填写tblBooking
表。
I also did try joining them, but it doesn't work. 我也尝试过加入他们,但是没有用。 Thanks in advance for the reply. 预先感谢您的答复。
This is the code that I've made. 这是我编写的代码。 The problem is, it is double inserting the information in tblCustomers table. 问题是,它是在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
You query2
is not specyfing values: 您query2
不是指定值:
query2 = "INSERT INTO tblBooking WHERE Customer_ID = tblCustomer.Customer_ID, Day_In = '" & dayIn.Value & "', Day_Out = '" & dayOut.Value & "', Room_ID = '" & rmIDlbl.Text & "'"
as you do in query1. 就像在query1中一样。 You are writing the where clause but you are not writing values. 您正在编写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 & "')"
It should be: 它应该是:
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 & "'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.