-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLHandle.cs
93 lines (89 loc) · 5.07 KB
/
SQLHandle.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
namespace Network_Tool
{
public partial class Component3 : Component
{
/// <summary>
/// the namespace acts as an identifier for all components of this program to keep them isolated from other programs
/// Seen above are all the class libraries imported for each sub class. these are inherited but I have overridden them in the other components to increase efficiency
/// </summary>
public Component3()
{
InitializeComponent();
}
public Component3(IContainer container)
{
//this container function acts to encapsulate programs executed inside of it
//The component is added to the encapsulation and initialized again to reset it in the isolated environment
container.Add(this);
InitializeComponent();
}
public static void Times(string address, string interval, int tick)
{
//function to take the data from the volatile database and combine it with metadata ready to be added to the main database "DATA"
Component4.Traceroute(address, interval, tick);
}
public static void runtime(DataTable hops, int count, string target, int tick)
{
//data coming from the datatable "dat" is sorted into ascending order
DataView sorted = new DataView(hops);
sorted.Sort = "hop ASC";
SqlConnection connection = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
//iterating through the table to select the rows of data and add them, along with extra data, to the main database
for (int Hopnum = 0; Hopnum < count; Hopnum++)
{
DataRow[] test;
string query = "hop = "+(Hopnum).ToString();
test = sorted.Table.Select(query);
foreach(DataRow row in test)
{
if(row[0].ToString() != "")
{
//collecting the data from the table and formatting it for further use
//this separates the data from a known structure and places it into variables for ease of access
string hop = row[0].ToString();
string Adress = row[1].ToString();
int sent = Convert.ToInt32(row[2]);
int received = Convert.ToInt32(row[3]);
int ping = received - sent;
int PL = 0;
string date = DateTime.Now.Date.ToString();
string time = DateTime.Now.Hour.ToString();
int packetloss = Convert.ToInt32(row[4]);
if (hop == "X")
{
PL = 100;
}
//the code responsible for passing the data to the database, all of the data is entered as a parameter of the base command
SqlDataAdapter DataTable = new SqlDataAdapter();
DataTable.InsertCommand = new SqlCommand("insert into Data(Host, send, receive, hop, loss, date, time, seq, packetloss, Code) values (@host, @send, @receive, @hop, @loss, @date, @time, @seq, @packetloss, @code)", connection);
DataTable.InsertCommand.Parameters.Add("@host", SqlDbType.Text).Value = target;
DataTable.InsertCommand.Parameters.Add("@seq", SqlDbType.Int).Value = Hopnum;
DataTable.InsertCommand.Parameters.Add("@send", SqlDbType.Text).Value = sent;
DataTable.InsertCommand.Parameters.Add("@receive", SqlDbType.Text).Value = received;
DataTable.InsertCommand.Parameters.Add("@hop", SqlDbType.Text).Value = Adress;
DataTable.InsertCommand.Parameters.Add("@loss", SqlDbType.Text).Value = PL;
DataTable.InsertCommand.Parameters.Add("@date", SqlDbType.Text).Value = date;
DataTable.InsertCommand.Parameters.Add("@time", SqlDbType.Text).Value = time;
DataTable.InsertCommand.Parameters.Add("@packetloss", SqlDbType.Int).Value = packetloss;
DataTable.InsertCommand.Parameters.Add("@code", SqlDbType.Int).Value = tick;
//executing the SQL command, Error trap in case of an closed connection
try
{
DataTable.InsertCommand.ExecuteNonQuery();
}
catch
{
connection.Open();
DataTable.InsertCommand.ExecuteNonQuery();
}
connection.Close();
}
}
}
}
}
}