[英]How do I store data from an ObservableCollection into an SQL database?
I have some records inside an ObservableCollection. 我在ObservableCollection中有一些记录。
void proxy_AddPayNowOrderCompleted(object sender, AddPayNowOrderCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
ObservableCollection<Product> Products = this.listBox1.ItemsSource as ObservableCollection<Product>;
}
My Product entity will have prodid, prodname, prodrice. 我的产品实体将具有prodid,prodname,prodrice。 There will be many of these entities inside the ObservableCollection.
ObservableCollection中将有许多这样的实体。 I want to save all the records in the ObservableCollection into my SQL database.
我想将ObservableCollection中的所有记录保存到我的SQL数据库中。 I'm using this code to save one record into the database.
我正在使用此代码将一条记录保存到数据库中。
private void btn1_Click(object sender, RoutedEventArgs e)
{
proxy.AddPayNowOrderCompleted += new EventHandler<AddPayNowOrderCompletedEventArgs>(proxy_AddPayNowOrderCompleted);
proxy.AddPayNowOrderAsync(orderdate, prodid, orderprodname, orderprice, orderstatus, custemail, paymentdate);
}
How can I store all of the records inside the ObservableCollection and insert them into the database? 如何将所有记录存储在ObservableCollection中并将它们插入数据库?
Something like this should work: 这样的事情应该有效:
ObservableCollection<Product> Products = this.listBox1.ItemsSource as ObservableCollection<Product>;
proxy.AddPayNowOrderCompleted += new EventHandler<AddPayNowOrderCompletedEventArgs>(proxy_AddPayNowOrderCompleted);
foreach (Product p in Products)
{
proxy.AddPayNowOrderAsync(p.orderdate, p.prodid, p.orderprodname, p.orderprice, p.orderstatus, p.custemail, p.paymentdate);
}
Your service may have a method that allows you to pass in multiple products at once, which would be much more efficient than making all those separate round trips, but without more information about the members in your proxy class, that's impossible to determine. 您的服务可能有一种方法允许您一次传入多个产品,这比进行所有这些单独的往返更有效,但是如果没有关于代理类中成员的更多信息,则无法确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.