c# – 为什么我不能使用LINQ to SQL将数据更新到数据库中?
发布时间:2020-12-15 04:10:44 所属栏目:百科 来源:网络整理
导读:我正在尝试在从数据库中读取数据时更新数据,请参阅下文. 但在整个事情结束后,数据没有得到更新. 是否需要指定任何事务语法? (当我调试时,我可以看到我检索到了正确的记录.) using (conn = new SqlConnection(MyConnectionString)) using (SqlCommand cmd =
我正在尝试在从数据库中读取数据时更新数据,请参阅下文.
但在整个事情结束后,数据没有得到更新. 是否需要指定任何事务语法? using (conn = new SqlConnection(MyConnectionString)) using (SqlCommand cmd = new SqlCommand("dbo.MyProcedure",conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Count",count); conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while (reader.Read()) { // wrapper object,not related to database SampleModel c = new SampleModel(); c.ID= (string)reader["ID"]; c.Name = (string)reader["Name"]; c.Type = (int)reader["Type"]; // modeList will return to outside,not related to database modelList.Add(c); sampleTable1 table1 = context.sampleTable1s.SingleOrDefault(t=> t.id = c.ID); // try to update the "isRead",but it doesn`t work....!!! // about the datatype,in the data base,it is "smallInt" // in linq to sql,it is "short?" // PS Default value all should be 0 table1.isRead = 1; context.SubmitChanges(); <--- here,it doesn`t do the job // context is new from Linq to SQL } } conn.Close(); } 这是我的程序: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE MyProcedure @Count int = 100 AS BEGIN SELECT TOP (@Count ) t1.id AS ID,t1.name AS Name,t2.type AS TYPE FROM sampleTable1 as t1 with (nolock),sampleTable2 as t2 with (nolock) WHERE (t1.t2Id = t2.Id) ORDER BY t1.name asc END GO 如果我将所有代码放在TransactionScope块中 using (TransactionScope scope = new TransactionScope()) { // all the C# code above scope.Complete(); } 我将得到一个例外“服务器上的MSDTC’localhost-sqlserver2005’不可用.” 如果我只放了一些代码,没有例外,但数据确实没有更新 using (TransactionScope scope = new TransactionScope()) { sampleTable1 table1 = context.sampleTable1s.SingleOrDefault(t=> t.id = c.ID); table1.isRead = 1; context.SubmitChanges(); scope.Complete(); } 谢谢. 解决方法
检查所讨论的实体类中的至少一个成员是否被标记为L2S设计器中的主键成员.如果一个实体没有任何PK成员,L2S会在提交更新时默默地忽略它(默认情况下不会抛出异常,也不会生成更新的SQL语句并发送给db).
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ORA-00979:不是Oracle的GROUP BY表达式,但在子句差异的上下
- MTV和MVC Ajax contentType 序列化组件 分页器
- c – 一个朋友类对象访问基类的派生类对象的私有成员?
- ruby-on-rails – Ruby – 如何从ruby上的.pfx文件中提取pu
- c# – 格式化DateTime错误“方法’ToString’没有重载需要1
- C/C++指针,ptr 1 = ptr 1字节或ptr 1 * sizeof(pointer_typ
- [数据结构与算法]用断言修改正则表达式
- Shark源码分析(三):数据预处理之正则化
- ruby-on-rails-3 – 门卫访问令牌
- Xml:MalformedByteSequenceException: Invalid byte 2 of 2