使用LINQ从数据库进行数据更新

使用LINQ从数据库进行数据更新

本文介绍了使用LINQ从数据库进行数据更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有简单的代码,其中在``特定''列中插入了数据,我在代码中放置了一个检查,如果CheckInTime字段已填充,然后使用当前系统日期时间填充Checkout字段,但它仅填充了CheckInTime列和If再次单击,然后单击id添加另一行,但我想更新该行.
期待指导
以下是我拥有的代码

Hi,
I have simple code in which I have Inserted data in the Specific column,I have placed a check here in the code that If CheckInTime field is filled Then fill the Checkout Field with current system Date time.But It just fill The checkInTime column and If click again then id Add Another row But I want to update that row.
Looking forward for guidance
Following is the code that I have

protected void imgbtn_Click(object sender, ImageClickEventArgs e)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            CheckInCheckOut chkInOut = new CheckInCheckOut();
            if (chkInOut.CheckInTime != null)
            {
                chkInOut.CheckOutTime = DateTime.Now;

            }
            else
            {
                chkInOut.CheckInTime = DateTime.Now;
                db.CheckInCheckOuts.InsertOnSubmit(chkInOut);
            }
            chkInOut.Name = txtName.Text;

            db.SubmitChanges();

推荐答案

CheckInCheckOut chkInOut = db.CheckInCheckOuts.GetChange(txtName.Text); // if txtName.Text is the key value
if (chkInOut != null)
{
    chkInOut.CheckOutTime = DateTime.Now;
}
else
{
    chkInOut = new CheckInCheckOut();
    chkInOut.CheckOutTime = DateTime.Now;
    chkInOut.Name = txtName.Text;
    db.CheckInCheckOuts.InsertOnSubmit(chkInOut);
}
db.SubmitChanges();



问候
Piet



Regards
Piet


这篇关于使用LINQ从数据库进行数据更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:24