title: Updating a DB (SQL Server) using Ids from a dataTable in C# without For loop
body: <p>its my first question on S.O, also I'm kinda new in c# and SqlServer. 
  so pretty please be nice.</p>
  
  <p>I have a dataTable with 4 columns: id1, id2, id3, id4 (and several rows)
  also I have a SQL Server DB containing the 4 columns and additional columns, 2 of them named field1 and field2 </p>
  
  <p>I want to Update field1 and field2 to null in the db (without using foreach Row)</p>
  
  <p>I've succeeded updating the first row using this: </p>
  
  <pre><code>public void UpdateDt(DataTable dt, string ConnectionString)
  {
      Conn = null;
      DataRow row =  dt.Rows[0]   
      try
      {
  
          Conn = new SqlConnection(ConnectionString);
          Conn.Open();
          string sql = "update [DB].[dbo].[MyTable] set [field1] = null,"+
                  "[field2] = null where [ID1] = '"+row["ID1"]+"' and [ID2] = '"+row["ID2"]+"' "+
                  "and [ID3] = '"+row["ID3"]+"' and [ID4] = '"+row["ID4"]+"'";
          var sqlDataAdapter = new SqlDataAdapter { UpdateCommand = Conn.CreateCommand() };
          sqlDataAdapter.UpdateCommand.CommandText = sql;
          sqlDataAdapter.UpdateCommand.BeginExecuteNonQuery();
      }
      catch( Exception ex) log(ex);
      finally
      { 
          if (Conn != null)
          {
              Conn.Close();       
              Conn.Dispose(); 
          }
      }
  }
  </code></pre>
  
  <p>how can I update using the entire DataTable?</p>
  
  <p>please help
  thanks in advance</p>
  
  <p>-- response to comment by  Soner Gönül</p>
  
  <p>I've changed the sample code according to your advice, thanks</p>
  
  <p>Zohar,thanks,  I've chosen the fields names in order to try and make the question more general and understandable</p>
  
  <p>---edit---
  I've added a code for updating the entire DataTable using ForEach (with the help of Russ (thanks Russ)) </p>
  
  <pre><code> bool UpdateDt(DataTable dt)
          {
              Conn = null;
              try
              {
                  Conn = new SqlConnection(ConnectionString);
                  Conn.Open();
                  string sql = "update [KeywordInjectionData] " +
                                                "set " +
                                                "[field1] = null " +
                                                ",[field2] = null " +
                                                "where " + 
                                                "[Id1] = @Id1" +
                                                " and [Id2] = @Id2" +
                                                " and [Id3] = @Id3" +                                                                   
                                                " and [Id4] = @Id4;" ;
  
  
                  var sqlDataAdapter = new SqlDataAdapter { UpdateCommand = Conn.CreateCommand() };
                  foreach (DataRow row in dt.Rows)
                  {
                      sqlDataAdapter.UpdateCommand.CommandText = sql;
                      sqlDataAdapter.UpdateCommand.Parameters.AddWithValue("@Id1",row["Id1"]);
                      sqlDataAdapter.UpdateCommand.Parameters.AddWithValue("@Id2",row["Id2"]);
                      sqlDataAdapter.UpdateCommand.Parameters.AddWithValue("@Id3",row["Id3"]);
                      sqlDataAdapter.UpdateCommand.Parameters.AddWithValue("@Id4",row["Id4"]);
                      sqlDataAdapter.UpdateCommand.ExecuteNonQuery();
                      sqlDataAdapter.UpdateCommand.Parameters.Clear();
                  }
              }
              catch (Exception e1)
              {
                  Utillties.LogError(e1);
                  return false;
              }
  
              finally
              {
                  if (Conn != null)
                  {
                      Conn.Close();
                      Conn.Dispose();
                  }
              }
              return true;
          }
  </code></pre>
  
  <p>but the question remains, how can I update the DB without a ForEach loop?</p>