1

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

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();
}
}

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

4

1 回答 1

1

尝试这个:

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

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

var query = (from r in dataContext.YourTable
             where r.accountType == accounttype &&  r.ID == Id 
             select r);
于 2013-03-11T12:39:40.660 回答