-
Notifications
You must be signed in to change notification settings - Fork 1
/
Azure-Webserver-MSSQL-Data-Cleaner.cs
77 lines (71 loc) · 3.6 KB
/
Azure-Webserver-MSSQL-Data-Cleaner.cs
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
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create variables to read data into
Int32 ID_result;
DateTime LoggedTime_result;
string JSONData;
string JSONData2;
Int32 Device_ID;
// Try to access the Microsoft SQL database
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "DATASOURCE_NAME.database.windows.net";
builder.UserID = "DB_USERNAME";
builder.Password = "DB_PASSWORD";
builder.InitialCatalog = "DB_NAME";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
Console.WriteLine("\nQuery data example:");
Console.WriteLine("=========================================\n");
connection.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT [Id], [LoggedTime], [Data], [LoggingDeviceId] "); // To select all entries
sb.Append("FROM [dbo].[LoggedData];");
String sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
JSONData2 = "";
while (reader.Read())
{
ID_result = reader.GetInt32(0);
LoggedTime_result = reader.GetDateTime(1);
JSONData = reader.GetString(2);
Device_ID = reader.GetInt32(3);
// Remove "{"records":[" and "]}"
JSONData2 = JSONData2 + JSONData.Replace(@"{""records"":[", "").Replace(@"]}", "");//.Replace(@"},", "},\n");
//
// TODO: IDENTIFY RECORDS ALREADY READ IN (BY ID???), INCLUDE LOGGER ID AND OTHER INFO IN OUTPUT...
// ACCOMPLISH THIS BY LOOPING A QUERY??? THIS ISN'T NECESSARY IF YOU HAVE ONLY ONE C5 CELLULAR LOGGER.
// => while ID id the same, write to a filename with the logger ID appended, then switch to new file when that changes
// => Another feature could be to delete entries from the database once they've been written to the CSV file
//
//
}
// Properly paste JSON entries together, such that each line has the timestamp, name, and value,
// and is seperated by a comma and newline character
JSONData2 = JSONData2.Replace(@"},", "},\n").Replace(@"}{", "},\n{").Replace(@"},{", "},\n{");
System.IO.File.WriteAllText(@"C:\Users\dmero\Desktop\WriteText.txt", JSONData2);
Console.WriteLine("Process Complete: Press any key to exit.");
Console.ReadKey();
}
}
}
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}
}