Convert Empty String to NULL Data Table

  • Thread starter Thread starter Priya Bange
  • Start date Start date
P

Priya Bange

Guest
Hi Experts,

Am currently dynamically reading a csv file and loading into different tables of SQL Server using bulk copy method below.

The Csv contains empty string. Is it possible to convert them to NULL before performing an insert. An example code snipt below.



using (SqlConnection cnn = new System.Data.SqlClient.SqlConnection(connectionString))
{

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.CheckConstraints))
{
cnn.Open();

bulkCopy.DestinationTableName = $"dbo.{tableName}";
DataTableReader reader = dataTable.CreateDataReader();

bulkCopy.WriteToServer(reader);
cnn.Close();
}

}

Thanks Priya

Continue reading...
 
Back
Top