-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (87 loc) · 3.78 KB
/
Copy pathProgram.cs
File metadata and controls
103 lines (87 loc) · 3.78 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
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net;
using System.Net.Http;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.Data;
namespace Dynamics365DataExtractor
{
class Program
{
public static string aadTenant = ""; //https://login.windows.net/onCompanyName.onmicrosoft.com;
public static string aadClientAppId = ""; //AppGUID;
public static string aadClientAppSecret = ""; //AppSecret;
public static string aadResource = ""; //https://CompanyName.operations.dynamics.com;
public static string connString = "Server=CompanyServer;Database=CompanyDatabase;Trusted_Connection=True;";
public static string[] entities = new string[] { "LoanedEquipments", "LoanItems", "Positions", "Workers", "People" };
static void Main(string[] args)
{
AuthenticationResult token = Authenticate();
foreach (string entity in entities)
{
GetEntity(token, entity);
}
}
static AuthenticationResult Authenticate()
{
AuthenticationContext authenticationContext = new AuthenticationContext(aadTenant, false);
AuthenticationResult authenticationResult;
var credential = new ClientCredential(aadClientAppId, aadClientAppSecret);
authenticationResult = authenticationContext.AcquireTokenAsync(aadResource, credential).Result;
return authenticationResult;
}
static void GetEntity(AuthenticationResult token, string entity)
{
using (var client = new HttpClient())
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.AccessToken);
client.DefaultRequestHeaders.Add("Accept", "application/json;odata.metadata=none");
string entityURI = aadResource + "/data/" + entity;
var response = client.GetAsync(entityURI).Result;
string result = response.Content.ReadAsStringAsync().Result;
Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.Parse(result);
DataTable dtValue = (DataTable)JsonConvert.DeserializeObject(jobject.Property("value").Value.ToString(), (typeof(DataTable)));
string tableName = "dbo." + entity;
BulkCopy(dtValue, tableName);
}
}
static void ExecuteSQL(string sql)
{
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.ExecuteScalar();
connection.Close();
}
}
static void TruncateTable(string tableName)
{
string sql = "TRUNCATE TABLE " + tableName;
ExecuteSQL(sql);
}
static void BulkCopy(DataTable dt, String tableName)
{
TruncateTable(tableName);
using (SqlConnection connection = new SqlConnection(connString))
{
SqlBulkCopy bulkCopy =
new SqlBulkCopy
(
connection,
SqlBulkCopyOptions.TableLock |
SqlBulkCopyOptions.FireTriggers |
SqlBulkCopyOptions.UseInternalTransaction,
null
);
bulkCopy.DestinationTableName = tableName;
connection.Open();
bulkCopy.WriteToServer(dt);
connection.Close();
}
dt.Clear();
}
}
}