Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. public class SqlBulkWriter : ISqlBulkWriter
  2. {
  3. private readonly string _dbConnectionString;
  4. private const int BULK_INSERT_TIMEOUT_IN_SEC = 300;
  5. private const String APOSTROPHE_DELIMINATOR = "'";
  6.  
  7. /// <summary>
  8. /// Constructor
  9. /// </summary>
  10. /// <param name="dbConnectionString">Database Connection String</param>
  11. public SqlBulkWriter(string dbConnectionString)
  12. {
  13. Contracts.CheckNonEmpty(dbConnectionString, nameof(dbConnectionString));
  14.  
  15. this._dbConnectionString = dbConnectionString;
  16. }
  17. public void EmptyTempTable(string schema, string tableName)
  18. {
  19. Contracts.CheckNonEmpty(schema, nameof(schema));
  20. Contracts.CheckNonEmpty(tableName, nameof(tableName));
  21.  
  22. // validate table and schema name
  23. if(tableName.Contains(APOSTROPHE_DELIMINATOR) || schema.Contains(APOSTROPHE_DELIMINATOR))
  24. {
  25. throw new Exception($"Schema: {schema} Or Table: {tableName} contain an invalid name. Cannot contain '.' in the name.");
  26. }
  27.  
  28. using (var connection = new SqlConnection(this._dbConnectionString))
  29. {
  30. try
  31. {
  32. connection.Open();
  33. using (var cmdTruncate = new SqlCommand($"TRUNCATE TABLE [{schema}].[{tableName}] ", connection))
  34. {
  35. cmdTruncate.ExecuteNonQuery();
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. throw new Exception($"Exception when trying to truncate staging table: {tableName}", ex);
  41. }
  42. finally
  43. {
  44. connection.Close();
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment