如何使用LINQ C#将记录存入数据库# [英] How to isnert a record into a database using LINQ C#

查看:111
本文介绍了如何使用LINQ C#将记录存入数据库#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将记录插入到数据库内的表中。



下面是我得到通用错误消息的代码Error at此请求:



I am having great difficulty inserting a record into a table inside of a database.

Below is the code in which im getting the generic error message "Error at this request":

context.SaveChanges();







以下是整个班级的代码。请记住,Product和ProductOptions视图源都可以从上一个窗口加载产品的详细信息。



我基本上希望在点击购买按钮后将订单中的详细信息输入到orderViewSource中。但事实证明这是非常困难的。



以下是全班的代码,祝你好运。






Below is the code for the entire class. bear in mind the Product and ProductOptions viewsource are both there to load the details of the product from the previous window.

I basically want the details being entered into ordersViewSource to be inserted into the database once the Buy button is clicked. But it has proven to be very difficult.

Below is the code of the entire class, good luck.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Data.Services.Client;
using iShopClient.iShop;

namespace iShopClient.CustomerFolder
{
    /// <summary>
    /// Interaction logic for OrderItemWindow.xaml
    /// </summary>
    public partial class OrderItemWindow : Window
    {
        public OrderItemWindow()
        {
            InitializeComponent();
        }

        private iShopEntities context;

        private Uri svcUri = new Uri("http://localhost:29875/iShop.svc");

        System.Windows.Data.CollectionViewSource productsViewSource;
        System.Windows.Data.CollectionViewSource productOptionsViewSource;
        System.Windows.Data.CollectionViewSource ordersViewSource;

        Product product;

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            productsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productsViewSource")));
            productOptionsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productOptionsViewSource")));
            ordersViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ordersViewSource")));

            context = new iShopEntities(svcUri);
            context.IgnoreMissingProperties = true;


            var query = context.CreateQuery<Product>("GetProductDetails").AddQueryOption("ProductID", GlobalVariables.productID);

            var query2 = context.CreateQuery<ProductOption>("GetProductOptionDetails").AddQueryOption("OptionID", GlobalVariables.optionID);

            productsViewSource.Source = new DataServiceCollection<Product>(query);
            productOptionsViewSource.Source = new DataServiceCollection<ProductOption>(query2);


            //var query3 = context.CreateQuery<Customer>("GetCustomers").AddQueryOption("customerNo", GlobalVariables.customerNo);

            //ordersViewSource.Source = new DataServiceCollection<Customer>(query3)

            ordersViewSource.Source = new DataServiceCollection<Order>(context.Orders);
            ListCollectionView orderList = (ListCollectionView)ordersViewSource.View;

            orderList.AddNewItem(new Order());
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void buyButton_Click(object sender, RoutedEventArgs e)
        {
            context.SaveChanges();
        }
    }
}

推荐答案

我不能那样做。你正在Windows上添加对象加载nad保存点击按钮。这段代码中的new Order()是什么
i don't thing you can do like that. you are adding object on windows load nad saving on button click. an what is "new Order()" in this code
orderList.AddNewItem(new Order());

。你试图添加空白对象。



见示例



. are you trying to add blank object.

see example

public void addProject()
{
     Project OProject = new Project();
     var context = new DbEntities();
     OProject.Name = "Project1";
     OProject.modificationDate = DateTime.Now;
     context.Project.Add(OProject);
     context.SaveChanges();
}





你必须在添加项目之前初始化对象的必需属性。



you have to initialize the object's required properties before adding item.


这篇关于如何使用LINQ C#将记录存入数据库#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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