簡體   English   中英

Linq to Sql一般幫助-插入語句

[英]Linq to Sql General Help - Insert Statement

我目前正在嘗試在Web服務中創建新訂單(如下所示),然后發送該數據以在數據庫中插入新行。 由於某種原因,我的DBML /數據上下文不允許我使用InsertOnSubmit。

有任何想法嗎? 我大約7個月沒有用過Linq到Sql了。

提前致謝。

[WebMethod]
    public string InsertOrderToDatabases()
    {
        //Start Data Contexts ------
        DataContext db = new DataContext(System.Configuration.ConfigurationManager.AppSettings["RainbowCMSConnectionString"]);
        DataContext dcSqlOES = new DataContext(System.Configuration.ConfigurationManager.AppSettings["OESConnectionString"]);

        //Get table from local database
        Table<Schedule> Schedule = db.GetTable<Schedule>();

        //Find last order number in databases
        var lastOrderNumber = from lOrder in Schedule
                              orderby lOrder.templ_idn descending
                              select lOrder.templ_idn;

        int firstOrderID;
        var firstOrder = lastOrderNumber.FirstOrDefault();
        firstOrderID = firstOrder.Value + 1;

        qrOrder qrOrd = new qrOrder
        {
            .... data in here creating a new order

        };

        //TODO: fix below with an insert on submit
        if (qrOrd != null)
        {
        //    **Schedule.InsertOnSubmit(qrOrd);**
        }
        //db.GetTable<Schedule>().InsertOnSubmit(qrOrd);

        try
        {
            //Submit the changes to the database
            db.SubmitChanges();
            return "Orders were sent to the databases.";
        }
        catch ()
        {

        }
    }

根據您的響應,看來您使用的是錯誤的表或錯誤的數據類型。 我還注意到,在聲明本地Schedule變量時,將其聲明為Table<Schedule>類型,這意味着它應包含Schedule實體,而不是qrOrder實體。

Table<TEntity>.InsertOnSubmit期望傳遞一個特定的強類型實體。在您的情況下,它期望的是Web_Service.Schedul‌e ,但是您試圖傳遞一個qrOrder

 Schedule.InsertOnSubmit(qrOrd);

該行不會將更改提交給關聯實體,請嘗試此操作

    db.Schedule.InsertOnSubmit(qrOrd);
    db.SubmitChanges();

你可以嘗試

db.GetTable(typeof(Schedule)).InsertOnSubmit(qrOrd);

要么

 db.GetTable(qrOrd.GetType()).InsertOnSubmit(qrOrd);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM