Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SqlBulkWriter : ISqlBulkWriter
- {
- private readonly string _dbConnectionString;
- private const int BULK_INSERT_TIMEOUT_IN_SEC = 300;
- private const String APOSTROPHE_DELIMINATOR = "'";
- /// <summary>
- /// Constructor
- /// </summary>
- /// <param name="dbConnectionString">Database Connection String</param>
- public SqlBulkWriter(string dbConnectionString)
- {
- Contracts.CheckNonEmpty(dbConnectionString, nameof(dbConnectionString));
- this._dbConnectionString = dbConnectionString;
- }
- public void EmptyTempTable(string schema, string tableName)
- {
- Contracts.CheckNonEmpty(schema, nameof(schema));
- Contracts.CheckNonEmpty(tableName, nameof(tableName));
- // validate table and schema name
- if(tableName.Contains(APOSTROPHE_DELIMINATOR) || schema.Contains(APOSTROPHE_DELIMINATOR))
- {
- throw new Exception($"Schema: {schema} Or Table: {tableName} contain an invalid name. Cannot contain '.' in the name.");
- }
- using (var connection = new SqlConnection(this._dbConnectionString))
- {
- try
- {
- connection.Open();
- using (var cmdTruncate = new SqlCommand($"TRUNCATE TABLE [{schema}].[{tableName}] ", connection))
- {
- cmdTruncate.ExecuteNonQuery();
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"Exception when trying to truncate staging table: {tableName}", ex);
- }
- finally
- {
- connection.Close();
- }
- }
- }
Add Comment
Please, Sign In to add comment