I've noticed that after running the following code, I receive a 'column X does not allow DBNull.Value' error
I've been considering adding an if else statement which relates to a column being null or white space, in order to populate a NULL string field with "NULL".
As somebody brand new to development, who did not write the original code, I was curious to know if this is a solution that you would agree with or whether there is a better/simpler way to eliminate the DBNull error?
using (sqlConn)
{
sqlConn.Open();
dropUpdateCmd.ExecuteNonQuery();
createUpdateCmd.ExecuteNonQuery();
using (var bulkCopy = new SqlBulkCopy(sqlConn))
{
foreach (DataColumn col in dt.Columns)
{
bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
bulkCopy.BulkCopyTimeout = 600;
bulkCopy.DestinationTableName = "Destination.GenericTable";
bulkCopy.WriteToServer(dt);
}
mergeCmd.ExecuteNonQuery();
dropUpdateCmd.ExecuteNonQuery();
sqlConn.Close();
}