title: Automatically add SQL datetime using SqlBulkCopy with C#
body: <p>I am using a <code>SqlBulkCopy</code> command to enter data into a sql table. I have the first column which is <code>CreatedDate</code>, I set the Default Value or Binding to <code>getdate()</code> without quotes. However, it appears that when <code>bulkCopy.WriteToServer(table);</code> is called the following error is thrown: </p>
  
  <blockquote>
    <p>The given value of type String from the data source cannot be
    converted to type datetime of the specified target column.</p>
  </blockquote>
  
  <p>The Inner Exception is </p>
  
  <blockquote>
    <p>Failed to convert parameter value from a String to a DateTime.</p>
  </blockquote>
  
  <p>I have set the Default Value or Binding for the <code>CreatedDate</code> columns to <code>getdate()</code>. Does this automatically insert the current date and time into the column when the <code>SqlBulkCopy</code> command is executed?</p>
  
  <p>Do I need to add the column <code>CreatedDate</code> to the <code>DataTable</code>? If I have columns that automatically get inserted do I need to map them or add them? If so, how would you add or map a column of the SQL data type <code>datetime</code>?</p>
  
  <pre><code>var conn = new SqlConnection(connectionString);
  var sessionId = Guid.NewGuid();
  
  var table = new DataTable(); // create the datatable
  
  table.Columns.Add("location", typeof (string));
  table.Columns.Add("product_name", typeof (string));
  table.Columns.Add("product_id", typeof (string));
  table.Columns.Add("product_price", typeof (string));
  table.Columns.Add("session_id", typeof (string));
  
  foreach (var pair in idsAndPrices) // then loop all dictionary entries and add the rows
  {
      table.Rows.Add(location, productName, pair.Key, pair.Value, sessionId);
  }
  
  using (var bulkCopy = new SqlBulkCopy(conn))
  {
      bulkCopy.DestinationTableName = "dbo.ProductInventory";
      conn.Open();
      bulkCopy.WriteToServer(table); // write to database
      conn.Close();
  }
  </code></pre>