【问题标题】:C# DataTable Update Multiple LinesC# DataTable 更新多行
【发布时间】:2012-02-09 16:11:51
【问题描述】:

如何使用数据表进行多次更新?

我发现了这个Update 1 row

我的代码:

public void ExportCSV(string SQLSyntax, string LeFile, bool Is_Ordre, int TypeDonne)
        {
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();


                    SqlDataAdapter da = new SqlDataAdapter(SQLSyntax, connectionWrapper.conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Emp");
                    DataTable dt = ds.Tables["Emp"];
                    CreateCSVFile(dt, LeFile, Is_Ordre, TypeDonne);

                    //Update all lines, it not save in Database
                    foreach (DataRow row in dt.Rows)
                    {
                        row["IS_IMPORT"] = true;
                    }
                }
            }
            catch (Exception excThrown)
            {
                throw new Exception(excThrown.Message);
            }



        }

问题是:

foreach (DataRow row in dt.Rows)
                        {
                            row["IS_IMPORT"] = true;
                        }

它不会将其保存到数据库中。

提前谢谢你, 史蒂夫

【问题讨论】:

  • 当心:这种方法会消耗大量内存。如果您的表的行数超过 1000 行(任意数),则会对内存产生很大影响。你为什么不移动到 SqlDataReader,它可以逐行读取,然后使用文件流将字符串行附加到 csv 文件

标签: c# datatable dataset


【解决方案1】:

您必须致电da.Update()

【讨论】:

    【解决方案2】:

    您正在更新内存中的值。 DataTable 类不是 sql 视图,而是内存表示。 Sql 数据适配器只复制数据。

    您必须将更改写回数据库。试试这个:

    public void ExportCSV(string SQLSyntax, string LeFile, bool Is_Ordre, int TypeDonne)
        {
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();
    
    
                    SqlDataAdapter da = new SqlDataAdapter(SQLSyntax, connectionWrapper.conn);
    
                    da.UpdateCommand = connectedConnection.CreateCommand();
                    da.UpdateCommand.XXXX = YYYY; // construct the SQL Command                    
    
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Emp");
                    DataTable dt = ds.Tables["Emp"];
                    CreateCSVFile(dt, LeFile, Is_Ordre, TypeDonne);
    
                    //Update all lines, it not save in Database
                    foreach (DataRow row in dt.Rows)
                    {
                        row["IS_IMPORT"] = true;
                    }
    
                    da.Update(dt);
                }
            }
            catch (Exception excThrown)
            {
                throw new Exception(excThrown.Message);
            }
        }
    

    这应该有效。

    【讨论】:

    • 感谢您的回复。它运作良好。只是好奇为什么我需要两者:da.UpdateCommand.CommandText = "UPDATE ORDRE SET IS_IMPORT = 'true'";foreach (DataRow row in dt.Rows) { row["IS_IMPORT"] = true; }
    • 您的 sql 查询更新表中的 所有 数据。您必须构建一个 SqlDataAdapter Update 方法可以使用的 sql 查询。当您编写row["IS_import"] = true; 时,它会将行标记为已更新。调用 SqlDataAdapter.Update 将获取表的所有更新行,然后发出相应的更新命令。建议:当你似乎发现了这些类时,给视觉工作室设计师一个机会为你构建一切。
    【解决方案3】:

    您需要首先将 DataAdapter 上的 UpdateCommand 属性设置为将执行以更新数据库中的行的 UPDATE 语句。

    然后,在更新 DataTable 中的值之后,您需要将其传递给 DataAdapter.Update()。 然后,这将为 DataTable 中的每个更新行执行 UpdateCommand。

    参考文献:

    MSDN - SqlDataAdapter.Update
    MSDN-SqlDataAdapter.UpdateCommand

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多