C# DataTable 更新多行 [英] C# DataTable Update Multiple Lines

查看:77
本文介绍了C# DataTable 更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can i do multiple update using datatable ?

我发现了这个更新 1 行

我的代码:

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

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

it not save it into database.

提前谢谢你,史蒂夫

推荐答案

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

You are updating the value in-memory. The DataTable class is not a sql view, but a memory representation. The Sql Data Adapter only copy the data.

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

You have to write back the changes to the DB. Try this :

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

这应该有效.

这篇关于C# DataTable 更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆