Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
90 lines (74 loc) · 2.4 KB
/
Copy pathProgram.cs
File metadata and controls
90 lines (74 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace BulkLoader
{
class Program
{
static void Main(string[] args)
{
try
{
Load(args[0]);
}
catch(Exception e)
{
Console.WriteLine("Error: {0}", e.ToString());
}
}
static void Load(string path)
{
// Load the file
var reader = new StreamReader(path);
// Get the Data Table to hold the rows
var datatable = GetDataTable();
// Setup our SQL Connection to our SQL Data Warehouse
var connection = GetConnection();
// Iterate through the file
string row;
while ((row = reader.ReadLine()) != null)
{
// Split the row by comma
var values = row.Split(',');
// Add the row values to the Data Table
datatable.Rows.Add(values[0], values[1], values[2]);
};
// Open the connection to SQL Data Warehouse
connection.Open();
// Create a Bulk Copy class
var bulkCopy = new SqlBulkCopy(connection);
// Define the target table
bulkCopy.DestinationTableName = "dbo.DimProducts";
// Write the rows to the table
bulkCopy.WriteToServer(datatable);
// Cleanup
connection.Close();
reader.Close();
datatable.Dispose();
}
static SqlConnection GetConnection()
{
var sb = new SqlConnectionStringBuilder();
sb.DataSource = "##LOGICAL SQL SERVER##.database.windows.net";
sb.InitialCatalog = "##DATABASE##";
sb.UserID = "##USERNAME##";
sb.Password = "##PASSWORD##";
return new SqlConnection(sb.ConnectionString);
}
static DataTable GetDataTable()
{
var dt = new DataTable();
dt.Columns.AddRange
(
new DataColumn[3]
{
new DataColumn("ProductId", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Description", typeof(string))
}
);
return dt;
}
}
}