简体   繁体   English

如何将ObservableCollection中的数据存储到SQL数据库中?

[英]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.

相关问题 如何存储ObservableCollection的集合 <T> 有不同类型的T? - How do I store a collection of ObservableCollection<T> with different types for T? 如何将SQL Server数据库中的数据放入TextBox? - How do I place data from the SQL Server database into a TextBox? 如何将电子邮件存储到SQL Server数据库中? - How do I store emails into my SQL server database? 如何从 C# 中的 ObservableCollection 读取数据 - How do you read data from an ObservableCollection in C# 如何使用linq to sql从数据库读取数据并将其存储在数组中? - How to read data from database and store it in array using linq to sql? 如何将来自外部 API 的数据存储到我的 MS SQL 数据库中 - How to store data from an external API into my MS SQL Database 如何使用来自 ObservableCollection 的 LINQ 查询返回元素? - How do I return a element using LINQ query from an ObservableCollection? 如何从ObservableCollection中获取IndexOf Removed Item - How do i get IndexOf Removed Item from ObservableCollection 如何将数据存储在基于服务的数据库中? - How do i store data in a service-based database? 如何使用嵌套的ObservableCollection将数据LINQ数据导入ObservableCollection? - How do you LINQ data into ObservableCollection with nested ObservableCollection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM