LINQ查询插入更新删除操作 [英] LINQ Query For Insert Update Delete Operation

查看:58
本文介绍了LINQ查询插入更新删除操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,如何在Asp.net中使用linq插入,更新,删除数据库中的数据.....

Please help, on how to use linq in Asp.net in inserting, updating, deleting data in the database.....

推荐答案

引用:
使用LINQ实体的CRUD操作 [带有ASP.NET Web应用程序的LINQ演示 [ ^ ]
http://msdn.microsoft.com/en-us/library/bb907622 (v = vs.100).aspx [此处 [
Refer:
CRUD Operations using LINQ Entities[^]
LINQ Demo with ASP.NET Web Application[^]
http://msdn.microsoft.com/en-us/library/bb907622(v=vs.100).aspx[^]

..and more you can get here[^]


希望您正在使用EF..

1.对于插入,您无需使用LINQ,
只需从Context中获取表的实例对象并添加表对象.

2.更新操作..
Hope you are using EF..

1. For insert you no need to go for LINQ,
just take instance object of table from Context and add table object.

2.Update operation..
protected void btnUpdate_Click(object sender, EventArgs e)
{
    int ContactID = Convert.ToInt32(Request.QueryString["ContactID"]); //This is contact reference ID, i am taking it from QS..
    ContactContext ctxContact = new ContactContext();
    tblContact Contact = ctxContact.tblContacts.SingleOrDefault(ID => ID.ID == ContactID); //Here ID is primary key of the table, This is for update reference.
    Contact.Name = txtName.Text;
    Contact.Address = txtAddress.Text;
    Contact.PhoneNo = Convert.ToInt64(txtPhone.Text);
    if (objDB.SaveChanges() == 1)    //Save method will return 1 if success..
    {
        //Success alert..
    }
}



3.删除操作..



3.Delete operation..

protected void btnDelete_RowCommand(object sender, EventArgs e)
{
    int ContactID = Convert.ToInt32(Request.QueryString["ContactID"]); //This is contact reference ID, i am taking it from QS..
    dbTestEntities objDB = new dbTestEntities();
    tblContact obj=objDB.tblContacts.SingleOrDefault(ID=>ID.ID==intContactID);
    objDB.tblContacts.DeleteObject(obj);
    if(objDB.SaveChanges()==1)    //Save method will return 1 if success..
    {
        //Success alert..
    }
}


这篇关于LINQ查询插入更新删除操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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