问题描述
大家好,
我今天有一个小问题,我希望有人能帮助我.
我有一个包含三个外键表的表.
看起来像这样:
订单:
TransactionNumber (字符串)
OrderDate (DateTime)
PaymentFK (表付款的前键)
UserFK (表用户的外键)
ShoppingCartFK (表购物车的外键)
我想要的是将ShoppingCart保存到订单中,所以代码如下:
Hello everyone,
I have a small issue today and I`m hoping if someone can help me about it.
I have a table which is consist of three foreign key table.
It look like this:
ORDERS:
TransactionNumber (string)
OrderDate (DateTime)
PaymentFK (Foreing key to table Payments)
UserFK (Foreign key to table Users)
ShoppingCartFK (Foreign key to table Shopping Cart)
What I want is to save ShoppingCart into orders, so this is the code:
public void SaveOrder()
{
using (DataContext dc = new DataContext())
{
var a = (from x in dc.ShoppingCarts
where x.cartID == Session["LogggedUser"].ToString()
select x).SingleOrDefault();
var b = (from y in dc.Users
where y.name + " " + y.lname == Session["LoggedUser"].ToString()
select y).SingleOrDefault();
Orders myOrder = new Orders();
myOrder.TransactionNumber = lbl_br.Text;
myOrder.OrderDate = DateTime.Parse(lbl_date.Text);
myOrder.UserFK = b.Id;
myOrder.ShoppingCartFK=a.id;
myOrder.PaymentFK = int.Parse(dp_py.SelectedItem.Value);
dc.Orders.InsertOnSubmit(myOrder);
dc.SubmitChanges();
}
}
因此,如果登录的用户仅在购物车中添加了一个产品,则此方法有效,但是,如果他添加了更多项目,则该程序将失败(当然).我已经尝试过这样的foreach循环:
So, this works if logged user added only one product in Shopping cart, but If he adds more items, the program fall (of course). I`ve tryed to do foreach loop like this:
foreach (ShoppingCart item in a)
{
}
但是,我不确定该如何实施.
有人有什么建议吗?是否可能,或者我应该在数据库中进行其他安排.
最好的问候,
A.
But, acctually I`m not sure how to implement it.
Anyone have any suggestions? Is it possible, or should I make different arrangement in my database.
Best regards,
A.
推荐答案
var a = from x in dc.ShoppingCarts
where x.cartID == Session["LogggedUser"].ToString()
select x;
然后
then
foreach (ShoppingCart item in a)
{
Orders myOrder = new Orders();
myOrder.TransactionNumber = lbl_br.Text;
myOrder.OrderDate = DateTime.Parse(lbl_date.Text);
myOrder.UserFK = b.Id;
myOrder.ShoppingCartFK=a.id;
myOrder.PaymentFK = int.Parse(dp_py.SelectedItem.Value);
dc.Orders.InsertOnSubmit(myOrder);
dc.SubmitChanges();
}
这篇关于插入数据库处理顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!