将阀门从ListView插入到Sql(使用linq到sql) [英] Insert valve from listview to Sql (using linq to sql)

查看:54
本文介绍了将阀门从ListView插入到Sql(使用linq到sql)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个不同的Listview,分别是Item,quantity和price.列表视图的所有项目都将从一个文本框中添加.我想知道如何将listview项目存储到我的SQl数据库中.这是我用来将项目添加到列表视图中的代码.

private void SalesAddItemBtn_Click(object sender, RoutedEventArgs e)
    {
        ListProductName.Items.Add(ComSalesProduct.Text);
        ListProductQuantity.Items.Add(txtSalesQuantity.Text);
        ListProductCost.Items.Add("RM " + txtSalesCost.Text+".00");
    }

先谢谢了.

解决方案

假设您的实体定义如下:

 public class Order
 {
       public string product;
       public string quantity;
       public string cost;
  }

您可以通过Zip合并列表并更新数据库:

 var orders = ListProductName.Zip(ListProductQuantity, (p, q) => new { Product = p, Quantity = q }).Zip(
            ListProductCost, (p, c) => new Order() { product = p.Product, quantity = p.Quantity, cost = c });


        db.Orders.InsertAllOnSubmit(orders);            
        db.SubmitChanges();

i have three different Listview which is Item,quantity and price. All the items for the listview will be added from a textbox. I would like to know how can i store the listview items into my SQl database. Here is the code i use to add item into the listview.

private void SalesAddItemBtn_Click(object sender, RoutedEventArgs e)
    {
        ListProductName.Items.Add(ComSalesProduct.Text);
        ListProductQuantity.Items.Add(txtSalesQuantity.Text);
        ListProductCost.Items.Add("RM " + txtSalesCost.Text+".00");
    }

Thanks in advance..

解决方案

Assuming your entity is defined something like this:

 public class Order
 {
       public string product;
       public string quantity;
       public string cost;
  }

You can Zip to combine the lists and update the database:

 var orders = ListProductName.Zip(ListProductQuantity, (p, q) => new { Product = p, Quantity = q }).Zip(
            ListProductCost, (p, c) => new Order() { product = p.Product, quantity = p.Quantity, cost = c });


        db.Orders.InsertAllOnSubmit(orders);            
        db.SubmitChanges();

这篇关于将阀门从ListView插入到Sql(使用linq到sql)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆