简体   繁体   中英

Selecting Multiple Tables in MS Access using C#

Hello Everyone I got a little confuse about selecting multiple fields from different tables binding it in one result, for you to understand it well, i posted my code & ERD below. thank you for your response. Please tell me where did I go wrong tnx :)

Here's the ERD of Mine:

在此处输入图片说明

and Here's the Code:

try
        {
            OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\MotoFix.mdb;");
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            Con.Open();
            command.CommandText = String.Format("SELECT fn.Customer + ' ' + mi.Customer + ' ' + ln.Customer as [CUSTOMER FULLNAME], fn.Cashier + ' ' + mi.Cashier + ' ' + ln.Cashier as [CASHIER FULLNAME], prodName.Product as [PRODUCT NAME], prodDescription.Product as [PRODUCT DESCRIPTION], prodBrand.Product as [PRODUCT BRAND], prodQuantity.Transaction as [PRODUCT QUANTITY], prodTotalPrice.Transaction as [SUBTOTAL], job.Personnel as [Referral] FROM Product , [Order] , [Transaction] , Customer , Cashier , Personnel  WHERE prodCode.Product = ProdCode.Order AND orderNo.Order = orderNo.Transaction AND pID.Personnel = pID.Transaction AND custID.Customer = custID.Transaction AND userID.Cashier = userID.Transaction AND sDate.Transaction = '{0}'", strDate);
            command.Connection = Con;
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            Con.Close();
            Con.Dispose();
            gridViewTransac_1.DataSource = dt;

        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message);
        }

Additional information: No value given for one or more required parameters.

I'm not a good sql query builder but shouldn't it be TableName.FieldName not the other way around ?

Anyways try to put the TableName first:

SELECT Customer.fn + ' ' + customer.mi + ' ' + customer.ln as [CUSTOMER FULLNAME]
     , Cashier.f. + ' ' + Cashier.mi + ' ' + customer.ln as [CASHIER FULLNAME]
     , Product.prodName as [PRODUCT NAME]
     , Product.prodDescription as [PRODUCT DESCRIPTION]
     , Product.prodBrand as [PRODUCT BRAND]
     , Transaction.prodQuantity as [PRODUCT QUANTITY]
     , Transaction.prodTotalPrice as [SUBTOTAL]
     , Personnel.job. as [Referral] 

FROM   Product 
     , [Order] 
     , [Transaction] 
     , Customer
     , Cashier 
     , Personnel  

WHERE Product.prodCode = Order.ProdCode 
  AND Order.orderNo = Transaction.orderNo 
  AND Personnel.pID= Transaction.pID 
  AND Customer.custID = Transaction.custID 
  AND Cashier.userID = Transaction.userID 
  AND Transaction.sDate = @transDate 

For @transDate you should add:

command.Parameters.AddWithValue("@transDate", someDate);

or you could simply do as you did

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