简体   繁体   English

如何将数据插入VB.net中的2个不同表中,我正在使用MS Access作为数据库

[英]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个表: tblCustomerstblBookingtblRoom

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.

相关问题 MS Access / VB.Net - 如何将Excel工作表数据中的行插入MS Access表? - MS Access/VB.Net - how can I insert rows from an Excel sheet data into MS Access table? 使用VB.NET 2008将.CVS数据插入Access DB - Insert .CVS data into access DB using VB.NET 2008 VB.NET:如何使用ExecuteSQLCommand并将数据返回到数据表中并进行访问? - VB.NET: How can I use ExecuteSQLCommand and return my data in a datatable and access it? VB.net-如何删除Access数据库中的项目? - VB.net - How can I delete an Item in an Access Database? 如何使用VB.NET在单个事务中插入SQL 2008父行和子行? - How can I insert SQL 2008 parent and child rows in a single transaction using VB.NET? vb.net MS Access使用SQL查询将具有自动编号主键列的记录插入表 - vb.net MS Access insert record with auto number primary key column into table with SQL query 使用ASP.net(VB.net)插入无重复的MS Access - Insert without duplicates MS Access using ASP.net (VB.net) 如何使用vb.net从Access db提取数据并将其放入文本框? - How to extract data from Access db and place it into a text box using vb.net? 无法从VB.NET插入MS Access数据库 - Cannot insert into MS Access database from VB.NET 使用MS-ACCESS作为数据库在VB.net中对Month + Year进行排序 - Sort Month+Year in VB.net using MS-ACCESS as db
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM