[英]How to retrieve data from more then two table of MS Access database using c#
我想从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]内部联接[OrderVT]上的[Customer_ID]内部联接。[Product_ID] = [ProductVT]。[Product_ID]'的语法错误(缺少运算符)
请帮忙。 提前致谢。
使用如下括号
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.