结果显示:
abc 1234
abc 123456说明没有把修改同步至数据库。怎么办?string strconn="Data Source=PC-201002231828\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;Pooling=False";
DataContext db = new DataContext(strconn);
Table<userinfo> table = db.GetTable<userinfo>();
var a = from sou in table
where sou.username == "abc"
select sou;
foreach (var sou2 in a)
{
sou2.userpassword = "1234";
Console.WriteLine("{0}{1}", sou2.username, sou2.userpassword);
}
db.SubmitChanges();
DataContext db1 = new DataContext(strconn);
Table<userinfo> table1 = db1.GetTable<userinfo>();
var a1 = from s in table1
where s.username=="abc"
select s;
foreach (var sou3 in a1)
{
Console.WriteLine("{0}{1}",sou3.username,sou3.userpassword);
}
Console.ReadLine();
abc 1234
abc 123456说明没有把修改同步至数据库。怎么办?string strconn="Data Source=PC-201002231828\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;Pooling=False";
DataContext db = new DataContext(strconn);
Table<userinfo> table = db.GetTable<userinfo>();
var a = from sou in table
where sou.username == "abc"
select sou;
foreach (var sou2 in a)
{
sou2.userpassword = "1234";
Console.WriteLine("{0}{1}", sou2.username, sou2.userpassword);
}
db.SubmitChanges();
DataContext db1 = new DataContext(strconn);
Table<userinfo> table1 = db1.GetTable<userinfo>();
var a1 = from s in table1
where s.username=="abc"
select s;
foreach (var sou3 in a1)
{
Console.WriteLine("{0}{1}",sou3.username,sou3.userpassword);
}
Console.ReadLine();
{
var dept = db.Depatment.Where(i => i.Id == 1).First();
dept.DeptName = "AAA";
Employee employee = new Employee() {Name = "AAA"};
dept.Employee.Add(employee);
db.SaveChanges();
}
{
var dept = db.Depatment.Where(i => i.Id == 1).First();
dept.DeptName = "AAA";
Employee employee = new Employee() {Name = "AAA"};
dept.Employee.Add(employee);
db.SaveChanges();
}
我是新手,完全看不懂。
where sou.username == "abc"
select sou;怀疑你更新的只是表数据的副本。
GetTable<userinfo> 改为 db.userinfohttp://stackoverflow.com/questions/4287983/working-with-datacontext-gettablet-to-get-a-queryprovider
Table<userinfo> table = db.userinfo
点不出userinfo的。
你那个Table其实是多此一举,查询的东西是从Table里面的,再更新到上下文
而上下文的db又没有改变,所以
如果使用table的话,使用这种方式table.Single(sou.username == "abc");
能跟我讲讲吗?
上面说的 直接使用db.userinfo。但是db点不出userinfo。
就能点出userinfo了。否则你现在连Insert都没有。
HOHO。这个原因很简单因为楼主没有定义Customer表格的主键, LINQ没有这个信息就无法准确的更新数据库内的对象,所以把你的操作忽略了。 修改的方法是把CustomerId字段改为如下: C# code
private int _customerId;[Column(Storage = "_customerId", DbType = "Int NOT NULL", IsPrimaryKey = true)]http://topic.csdn.net/u/20080823/05/fc9e1a93-153d-4dcc-ad52-a62b850fabae.html
用SQL Sever直接把username做主键,或者用其他办法。
做了主键以后 使用O/R设计器
把映射好的代码复制到程序中或者直接手工写红色代码
[Column(Storage = "username", DbType = "Int NOT NULL", IsPrimaryKey = true)]