Create table if not exists

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

Priya Bange

Guest
Hi Experts,

Is it possible to create a table out of the DataTable in case it doesn't exist in the destination before performing the SQLBulkCopy.

public static void DataToSQL(DataTable table, String tableName)
{
using (SqlConnection cnn = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("COnnetionDetails")))
{
cnn.Open();

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(cnn))
{

bulkCopy.DestinationTableName = $"dbo.{tableName}";
DataTableReader reader = table.CreateDataReader();
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}

Catch (Exception E)
{
Console.WriteLine (E);
}
}

Thanks in advance

Priya

Continue reading...
 
Back
Top