如何使用linq更新数据库到sql

时间:2021-01-23 23:06:23

I am trying to change a value stored in a database but I am unsure on how to do that,

我试图更改存储在数据库中的值,但我不确定如何做到这一点,

using (DataClassesDataContext dataContext = new DataClassesDataContext())
{        
int accounttype = 1;
int id = 123;
//Search database for record based on round and player nummber
var query = (from r in dataContext.Tables
            where r.accountType.Equals(accounttype)
            where r.ID.Equals(Id)
            select r);

//store player1s oppent in database
foreach (var record in query)
{
    record.fund = 1234;

    dataContext.Tables.InsertOnSubmit(record);
    dataContext.SubmitChanges();
}
}

so I'm trying to search the database for a particular record and change one of its values but I cannot seem to work out how to update it, I know that "InsertOnSubmit" is wrong though.

所以我试图在数据库中搜索特定记录并更改其中一个值,但我似乎无法解决如何更新它,我知道“InsertOnSubmit”是错误的。

1 个解决方案

#1


1  

Try this:

尝试这个:

record.fund = 1234;
dataContext.Entry(record).State = EntityState.Modified;
dataContext.SaveChanges();

Also you may need to change your query like:

此外,您可能需要更改您的查询,如:

var query = (from r in dataContext.YourTable
             where r.accountType == accounttype &&  r.ID == Id 
             select r);

#1


1  

Try this:

尝试这个:

record.fund = 1234;
dataContext.Entry(record).State = EntityState.Modified;
dataContext.SaveChanges();

Also you may need to change your query like:

此外,您可能需要更改您的查询,如:

var query = (from r in dataContext.YourTable
             where r.accountType == accounttype &&  r.ID == Id 
             select r);