【问题标题】:How to retrieve data from more then two table of MS Access database using c#如何使用c#从两个以上的MS Access数据库表中检索数据
【发布时间】:2014-04-13 07:30:52
【问题描述】:

我想从 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].[Customer_ID]=[CustomerVT].[Customer_ID] 内部联接 [ProductVT] on [OrderVT].[Product_ID]=[ProductVT].[Product_ID]”中存在语法错误(缺少运算符)

请帮忙。提前致谢。

【问题讨论】:

    标签: c# winforms ms-access oledb


    【解决方案1】:

    像下面这样使用括号

    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]
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多