简体   繁体   English

c#linq更新数据库中的所有记录

[英]c# linq update all records in database

im trying to update all records in a sql table using the following code but no data is updated. 我试图使用以下代码更新sql表中的所有记录,但没有更新数据。 does anyone know why? 有谁知道为什么?

        using (DataContext db = new DataContext())
        {
            foreach (Item record in db.Items)
            {
                record.Description += "bla";
                db.SubmitChanges();
            }
        }

code for setter: setter的代码:

[Column(Storage="_Description", DbType="NVarChar(400) NOT NULL", CanBeNull=false)] 
public string Description
{ 
  get { return this._Description; }
  set { 
      if ((this._Description != value))
       { 
         this._Description = value; 
       }
      }
}

Out of curiosity, see if moving the SubmitChanges() outside of the loop makes a difference: 出于好奇,看看在循环之外移动SubmitChanges()是否有所不同:

        using (DataContext db = new DataContext())
        {
            foreach (Item record in db.Items)
            {
                record.Description += "bla";   
            }
            db.SubmitChanges();
        }

From the details of the setter you have posted in the comments, your Description property has not been correctly created for notification of property changes. 根据您在注释中发布的setter的详细信息,尚未正确创建Description属性以通知属性更改。 Did you write the property yourself or was it generated by the VS2008 tooling? 您是自己编写该属性还是由VS2008工具生成?

Your Item class (all Linq to Sql entities for that matter should implement INotifyPropertyChanging and INotifyPropertyChanged) which will give you both PropertyChanging event and PropertyChanged events, if you used the VS2008 tools you should get a couple of methods like the following in your entity classes: 你的Item类(所有Linq to Sql实体都应该实现INotifyPropertyChanging和INotifyPropertyChanged),这将为你提供PropertyChanging事件和PropertyChanged事件,如果你使用了VS2008工具,你应该在实体类中获得如下的几个方法:

    protected virtual void SendPropertyChanging(string propertyName)
    {
        if (this.PropertyChanging != null)
        {
            this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    protected virtual void SendPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Now within your setter of your property you should use these methods to raise the required events: 现在,在您的属性的setter中,您应该使用这些方法来引发所需的事件:

[Column(Name="Description", Storage="_Description",
                  DbType="NVarChar(400) NOT NULL", CanBeNull=false)]   
public string Description
{
     get { return this._Description; }
     set
     {
         if ((this._Description != value))
         {
             this.SendPropertyChanging("Description");
             this._Description = value;
             this.SendPropertyChanged("Description");
         }
     }
 }

I also noticed you don't have the Name property set in your column attribute so add it just in case (I have it included in my example assuming your column name is "Description"). 我还注意到你没有在你的列属性中设置Name属性,所以添加它以防万一(我的例子中包含它,假设你的列名是“描述”)。

Maybe you need to supply a connection string. 也许你需要提供一个连接字符串。

using (DataContext db = new DataContext(_MyConnectionString))
{
    foreach (Item record in db.Items)
    {
        record.Description += "bla";
    }
    db.SubmitChanges();
}

I've had some strange issues with the DataContext when not supplying a connection string. 当没有提供连接字符串时,我对DataContext有一些奇怪的问题。

我会指出你可以在循环关闭后提交SubmitChanges ...这不会解决你的问题,但它会有所帮助。

I realize this is a really old question, but it remains unanswered. 我意识到这是一个非常古老的问题,但仍然没有答案。

The answer provided by Simon Fox is technically correct, and the suggestion by BFree is a big time saver. Simon Fox提供的答案技术上是正确的,而BFree的建议可以节省大量时间。

Yet, if your table does not have a Primary Key specified in the table, the DBML object will not detect a change and does not know how to update your records. 但是,如果表中没有在表中指定主键,则DBML对象将不会检测到更改,也不知道如何更新记录。

截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM