I have a csv file and I need to update data in only a few columns in my sql database table. What is the best way to do this? I was thinking bulk import however, it will not let me do this without using all of the columns. I was thinking of using format file, but I wanted to know if this is the most efficient way.
Here is how I was trying it from my C# class:
/// <summary>
/// Update all of the PropertyDefinitions
/// </summary>
internal static void InsertPropertyDefinitions()
{
//
// Define the connection
//
SqlConnection insertConnection = null;
try
{
RetryStrategy retryStrategy = new Incremental(5, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(3000));
RetryPolicy retryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(retryStrategy);
retryPolicy.ExecuteAction(() =>
{
//
// Try to connect to the database
//
using (insertConnection = new SqlConnection(m_ConnectionString))
{
//
// Open the connection
//
insertConnection.Open();
//
// Get the insert command ready
//
using (SqlCommand insertRecordCmd = insertConnection.CreateCommand())
{
//
// Define the Insert command
//
insertRecordCmd.CommandText = @"
BULK INSERT dbo.[PropertyDefinition]
FROM '//my file path'
WITH(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
";
// Execute the INSERT command
insertRecordCmd.ExecuteNonQuery();
}
insertConnection.Close();
}
});
}
catch (Exception ex)
{
//
// This is unexpected so display full exception information
//
m_Log.WriteLine("Exception while creating table");
m_Log.WriteLine(ex.Message.ToString());
throw;
}
}