title: Create table based on columns from a CSV file
body: <p>I have existing code which creates a new table based on the columns of a file (DBF, Excel):</p>
  
  <pre><code>OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + tbXLSBrowse.Text + "';Extended Properties=\"Excel 12.0 xml;HDR=Yes;IMEX=1\"");
  
  OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", oConn); //change to the sheet name
  
  oConn.Open();
  DataTable dt = new DataTable();
  
  dt.Load(command.ExecuteReader());
  
  oConn.Close();
  
  DataTableReader reader = dt.CreateDataReader();
  
  myConnection = new SqlConnection(cString);
  myConnection.Open();
  
  // checking whether the table selected from the dataset exists in the database or not
  string exists = null;
  try
  {
      SqlCommand cmd = new SqlCommand("SELECT * FROM sysobjects where name = '" + tb.Text + "'", myConnection);
      exists = cmd.ExecuteScalar().ToString();
      //MessageBox.Show("EXISTS");
  }
  catch (Exception exce)
  {
      exists = null;
      //MessageBox.Show("DOESNT EXIST");
  }
  
  if (exists == null)
  {
      // selecting each column of the datatable to create a table in the database
      foreach (DataColumn dc in dt.Columns)
      {
          if (exists == null)
          {
              SqlCommand createtable = new SqlCommand("CREATE TABLE " + tb.Text + " (" + dc.ColumnName + " varchar(MAX))", myConnection);
              createtable.ExecuteNonQuery();
              exists = tbXLSTableName.Text;
          }
          else
          {
              SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + tb.Text + " ADD [" + dc.ColumnName + "] varchar(MAX)", myConnection);
              addcolumn.ExecuteNonQuery();
          }
      }
  }
  </code></pre>
  
  <p>The textboxes are as follows:</p>
  
  <pre><code>//tbXLSBrowse.Text = the excel file name;
  //tb.Text = user generated table name;
  </code></pre>
  
  <p>The above code is an example for the Excel file. I have a CSV that I am also trying to do the same for but not sure how to do it.</p>
  
  <p>I have the following code which reads each line in the CSV file and gets the fields for each line and adds it to a List array:</p>
  
  <pre><code>var lines = File.ReadLines(textBox1.Text);
  List&lt;string&gt; colArray = new List&lt;string&gt;();
  
  foreach (string line in lines) //for each line
  {
      using (TextFieldParser parser = new TextFieldParser(textBox1.Text))
      {
          parser.TextFieldType = FieldType.Delimited;
          parser.SetDelimiters(",");
  
          while (!parser.EndOfData) //while file is being read
          {
              string[] fields = parser.ReadFields();
  
              foreach (string field in fields) //for each column
              {
                  colArray.Add(field);
                  colArray.ToArray();
              }
  
              MessageBox.Show(colArray.Count + ""); //displays the count for the columns for each line
              colArray.Clear(); //clear the column to use it for next line
          }
      }
  }
  </code></pre>
  
  <p>How can I combine the code posted in the beginning with the above code to do the following:</p>
  
  <ul>
  <li>Read the CSV file</li>
  <li>Create a SQL table based on the first row count (which is the header for the file)</li>
  </ul>
  
  <p>Or is it not possible? The reason I am wanting to do this is because there are 329 columns for each line and if I am able to accomplish this with the code, it will save a lot of time in the long run.</p>
  
  <p>Would this site be of any help: <a href="http://www.codeproject.com/Articles/30705/C-CSV-Import-Export" rel="nofollow">CSV to SQL</a></p>