C# DataTable Update Multiple Lines

前端 未结 3 1209
春和景丽
春和景丽 2021-01-16 12:29

how can i do multiple update using datatable ?

i found this Update 1 row

my code:

public void ExportCSV(string SQLSyntax, string LeFile, bool         


        
3条回答
  •  难免孤独
    2021-01-16 12:36

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

    This should works.

提交回复
热议问题