简体   繁体   中英

Joining 2 tables in C# Access Database

I am creating a booking system for a university project and I'm trying to generate an invoice based off of different customers' bookings. I have a Bookings table and a Guests table within an Access database and I'm trying to find a way to merge these tables together using a Command from within my Connections class. Is there anyway to do a JOIN statement? I have a BookingID inside Bookings and a BookingsID_FK within Guests so that way there is at least some basic link.

Here is the first part of my connection class

    //Facade Class made to handle all connection's to Access Database properties
public class ConnectionClass
{
    OleDbConnection connection;
    OleDbCommand command;

    private void ConnectTo()
    {
        connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MarkB\Documents\visual studio 2015\Projects\Holiday_Reservation\Holiday_Reservation\Reservations.mdb;Persist Security Info=False");
        command = connection.CreateCommand();
    }

    public ConnectionClass()
    {
        ConnectTo();
    }

Am I right in thinking that I would use a statement such as...

try
{

command.CommandText = "SELECT * FROM Bookings JOIN Guests ON Bookings.BookingID = Guests.BookingID_FK;"
command.CommandType = CommandType.Text;
connection.Open();

OleDbDataReader reader = command.ExcecuteReader();

    While (reader.Read())
    {


    DO SOMETHING......

The basic form of your SELECT statement is correct, except that Access SQL does not support an unqualified JOIN keyword. You'll need to use INNER JOIN (or possibly LEFT JOIN or RIGHT JOIN ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM