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

Asp.net 如何使用linq到sql更新数据库,asp.net,database,sql-update,insertonsubmit,Asp.net,Database,Sql Update,Insertonsubmit,我试图更改存储在数据库中的值,但我不确定如何更改 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

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

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”是错误的。

尝试以下方法:

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);
试试这个:

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

不起作用,它说它需要一个“Entry”的使用引用:\n我使用的是Visual Studio,它不识别“and”作为关键字,请在没有第一个代码段中间行的情况下尝试它。而不是在foreach循环中写入
&,而是在提交时删除insert,并将提交更改移动到foreach循环之后。很抱歉,应该是
&
。您可能需要
System.Data.Entity
reference和inheritage不起作用,它说它需要一个用于“Entry”的using引用:\n我使用的是Visual Studio,它不识别“and”作为关键字,请在没有第一个代码段中间行的情况下尝试它。而不是在foreach循环中写入
&,而是在提交时删除insert,并将提交更改移动到foreach循环之后。很抱歉,应该是
&
。您可能需要
System.Data.Entity
引用和继承