title: How to take a CSV field and write to columns in SQL
body: <p>I have the following code which takes a CSV and writes to a console:</p>
  
  <pre><code>using (CsvReader csv = new CsvReader(
             new StreamReader("data.csv"), true))
      {
          // missing fields will not throw an exception,
          // but will instead be treated as if there was a null value
          csv.MissingFieldAction = MissingFieldAction.ReplaceByNull;
          // to replace by "" instead, then use the following action:
          //csv.MissingFieldAction = MissingFieldAction.ReplaceByEmpty;
          int fieldCount = csv.FieldCount;
          string[] headers = csv.GetFieldHeaders();
          while (csv.ReadNextRecord())
          {
              for (int i = 0; i &lt; fieldCount; i++)
                  Console.Write(string.Format("{0} = {1};",
                                headers[i],
                                csv[i] == null ? "MISSING" : csv[i]));
              Console.WriteLine();
          }
      }
  </code></pre>
  
  <p>The CSV file has 7 headers for which I have 7 columns in my SQL table.</p>
  
  <p>What is the best way to take each <code>csv[i]</code> and write to a row for each column and then move to the next row?</p>
  
  <p>I tried to add the <code>ccsv[i]</code> to a string array but that didn't work.</p>
  
  <p>I also tried the following:</p>
  
  <pre><code>SqlCommand sql = new SqlCommand("INSERT INTO table1 [" + csv[i] + "]", mysqlconnectionstring);
  sql.ExecuteNonQuery();
  </code></pre>
  
  <p>My table (<code>table1</code>) is like this:</p>
  
  <p><code>name     address     city     zipcode     phone     fax     device</code></p>