Guest User

Untitled

a guest
Aug 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.IO;
  5.  
  6. namespace ARGHGHGHGHG
  7. {
  8. internal class Program
  9. {
  10. static int progress = 0;
  11. private static int total = 0;
  12. private const int batchsize = 1000;
  13.  
  14. private static void Main()
  15. {
  16.  
  17. const string filename = "c:\\stuff\\x{0}.csv";
  18. Console.WriteLine("hi");
  19. using (var dbconn = new SqlConnection("string"))
  20. {
  21.  
  22. dbconn.Open();
  23.  
  24. var bulk = new SqlBulkCopy(dbconn, SqlBulkCopyOptions.KeepIdentity, null) { DestinationTableName = "perflog", NotifyAfter = batchsize, BatchSize = batchsize };
  25. bulk.SqlRowsCopied += bulk_SqlRowsCopied;
  26.  
  27. for (var x = 0; x <= 9; x++)
  28. {
  29. var currfilename = string.Format(filename, x.ToString("00"));
  30. Console.WriteLine("Reading Data from {0}", currfilename);
  31. var datatable = CsvReader.GetDataTable(currfilename);
  32. total = datatable.Rows.Count;
  33. Console.WriteLine("Done. {0} rows", total);
  34.  
  35. bulk.WriteToServer(datatable);
  36. }
  37. }
  38. }
  39.  
  40. static void bulk_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
  41. {
  42. progress += batchsize;
  43. Console.WriteLine("Done {0} out of {1}", progress, total);
  44. }
  45. }
  46. class CsvReader
  47. {
  48. public static DataTable GetDataTable(string filename)
  49. {
  50. var conn = new System.Data.OleDb.OleDbConnection(string.Concat("Provider=Microsoft.Jet.OleDb.4.0; Data Source = ", Path.GetDirectoryName(filename), "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""));
  51. conn.Open();
  52. var strQuery = string.Concat("SELECT * FROM [", Path.GetFileName(filename), "]");
  53. var adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
  54. var dataset = new DataSet("CSV File");
  55. adapter.Fill(dataset);
  56. return dataset.Tables[0];
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment