如何使用c#从两个以上的MS Access数据库表中检索数据

时间:2014-04-13 07:30:52

标签: c# winforms ms-access oledb

我想从3个相互连接的表中检索MS Access数据库中的数据 我在业务层

中编写了这段代码
public List<ProductOrderModel> Show()
{
         OleDbConnection cn;
         try
         {
             ProductOrderModel dtoobj = new ProductOrderModel();
             DataLayer dalobj = new DataLayer();
             OleDbCommand cmdshow = new OleDbCommand();

             cmdshow.CommandText = "select [CustomerVT].[Customer_Name],[OrderVT].[Order_Date],[ProductVT].[Price],[ProductVT].[Item_Name],[OrderVT].[Quantity],[OrderVT].[Order_ID] from  [OrderVT] inner join [CustomerVT] on [OrderVT].[Customer_ID]=[CustomerVT].[Customer_ID] inner join [ProductVT] on [OrderVT].[Product_ID]=[ProductVT].[Product_ID] ";

              List<ProductOrderModel> Ldemo = new List<ProductOrderModel>();

             return dalobj.executereader(Ldemo, cmdshow, "BillingData");
   }
   catch (Exception ex2)
   {
       throw new DataException("error....." + ex2.Message);
   }
}

ExecuteReader()的代码是..

public List<ProductOrderModel> executereader(List<ProductOrderModel> Ldemo, OleDbCommand cmdshow, string tablename)
{
        OleDbConnection cn;

        try
        {
            cn = this.getconnection();

            cmdshow.Connection = cn;
            cn.Open();

            OleDbDataReader rd = cmdshow.ExecuteReader();

            while (rd.Read())
            {
                ProductOrderModel dtoobj1 = new ProductOrderModel();

                dtoobj1.InvoiceNo = Convert.ToInt32(rd["Order_ID"].ToString());
                dtoobj1.CustomerName = rd["Customer_Name"].ToString();

                dtoobj1.ItemName = rd["Item_Name"].ToString();
                dtoobj1.Quantity = Convert.ToInt32(rd["Quantity"].ToString());
                dtoobj1.Price = Convert.ToInt32(rd["Price"].ToString());
                dtoobj1.OrderDate = Convert.ToDateTime(rd["Order_Date"].ToString());
                Ldemo.Add(dtoobj1);
            }

            cn.Close();
            return Ldemo;
        }
        catch (Exception ex2)
        {
            throw new DataException("error....." + ex2.Message);
        }
}

但显示错误

  

查询表达式'[OrderVT]中的语法错误(缺少运算符)。[CustomerVT]。[CustomerVT]。[CustomerVT]上的[Customer_ID]内连接[ProductVT]。[Product_ID] = [ProductVT]。[Product_ID]'

请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用如下的括号

select [CustomerVT].[Customer_Name],[OrderVT].[Order_Date],[ProductVT].[Price],[ProductVT].[Item_Name],[OrderVT].[Quantity],[OrderVT].[Order_ID] 
from  ([OrderVT] 
inner join [CustomerVT] on [OrderVT].[Customer_ID]=[CustomerVT].[Customer_ID])
inner join [ProductVT] on [OrderVT].[Product_ID]=[ProductVT].[Product_ID]