Linq to SQL - 以表格形式加载数据然后保存

时间:2013-04-16 16:00:39

标签: c# winforms linq-to-sql

我在弄清楚如何做一些看似直截了当的事情时遇到了一些麻烦,但在提交更改时需要一些帮助来确定我出错的地方。 这种窗体形式的基本前提是在数据库中查询表单加载中的特定信息,然后加载它以进行任何需要进行的更改。

它加载得很好,我仔细检查以确保主键存在(它是),但由于某种原因它不会保存。 tkts.Log = Console.Out;单击保存按钮时甚至不显示任何内容。我是一名初学程序员,我正在尝试使用Linq to SQL,所以如果有什么不对,请提供指示。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TicketLogger;


namespace TicketLogger
{
    public partial class TicketInfo : Form
    {
        ITDataClassesDataContext tkts = new ITDataClassesDataContext();
        private Int32 ticket_id = 0;

        public TicketInfo(Int32 TicketID = 0)
        {
            InitializeComponent();
            //Set local TicketID variable for use on load
            ticket_id = TicketID;
        }

        private void tBL_TICKET_HDRBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            //Save the new ticket to the database
            //I think the object Tickets contains the changes, not tkts, but how to access?
                try
                {
                    tkts.Log = Console.Out;
                    tkts.SubmitChanges();
                    tkts.Log.Close();
                }
                catch (Exception m)
                {
                    MessageBox.Show(m.ToString());
                }
        }

        private void TicketInfo_Load(object sender, EventArgs e)
        {

            tkts.Log = Console.Out;
            //Get the ticket ID and retrieve information
            var Ticket = from objTkts in tkts.TBL_TICKET_HDRs
                         where objTkts.TICKET_ID == ticket_id
                         select objTkts;
            this.tBL_TICKET_HDRBindingSource.DataSource = Ticket;
            tkts.Log.Close();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了为什么它不会更新。我从来没有传递过任何数据。 我在提交更改之前在try块中添加了此代码并且已经接受了它。

            TBL_TICKET_HDR tkthdr = tkts.TBL_TICKET_HDRs.Single(t => t.TICKET_ID == ticket_id);
            tkthdr.ISSUE_DESC = iSSUE_DESCTextBox.Text;

出于某种原因,我只是希望代码“知道”表单上的控件应该在哪里看。