-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cs
117 lines (104 loc) · 3.81 KB
/
Database.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Data.SqlClient;
namespace infoGrabber
{
public class Database
{
SqlConnection sqlConnection;
public Database()
{
try
{
sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString);
sqlConnection.Open();
}
catch(SqlException e)
{
Console.WriteLine("Sql error" + e.Message);
}
}
public void insertPC(PCData data)
{
try
{
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("INSERT INTO PC(pcSerial,pcVendor,pcModel,pcWindowsVersion,pcName,domain,assetName) " +
"VALUES('" + data.serialNumberPC + "','" + data.vendorPC + "','" + data.modelPC + "','" + data.version + "','" + data.systemName + "','" + data.domain +
"','" + data.assetName + "')"
, sqlConnection);
sqlCommand.ExecuteNonQuery();
}
catch (SqlException e)
{
SqlCommand sqlCommand = new SqlCommand("INSERT INTO Monitor(monitorModel, monitorSerial, monitorVendor) " +
"VALUES('" + "Generic Model" + "','" + "Generic Serial" + "','" + "Generic Vendor" + "')"
, sqlConnection);
sqlCommand.ExecuteNonQuery();
Console.WriteLine("Sql error from insertPC method" + e.Message);
}
finally
{
sqlConnection.Close();
}
}
public void insertMonitor(MonitorData data)
{
try
{
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("INSERT INTO Monitor(monitorModel, monitorSerial, monitorVendor) " +
"VALUES('" + data.modelM + "','" + data.serialNumberM + "','" + data.vendorM + "')"
, sqlConnection);
sqlCommand.ExecuteNonQuery();
}
catch (SqlException e)
{
SqlCommand sqlCommand = new SqlCommand("INSERT INTO Monitor(monitorModel, monitorSerial, monitorVendor) " +
"VALUES('" + "Generic Model" + "','" + "Generic Serial" + "','" + "Generic Vendor" + "')"
, sqlConnection);
sqlCommand.ExecuteNonQuery();
Console.WriteLine("Sql error from insertMonitor method" + e.Message);
}
finally
{
sqlConnection.Close();
}
}
public bool verifyPC(string serial)
{
try
{
SqlCommand sqlCommand = new SqlCommand("SELECT * FROM PC WHERE pcSerial ='" + serial + "'", sqlConnection);
if (sqlCommand.ExecuteReader().HasRows)
return true;
}
catch (SqlException e)
{
Console.WriteLine("Sql error" + e.Message);
}
finally
{
sqlConnection.Close();
}
return false;
}
public bool verifyMonitor(string serial)
{
try
{
SqlCommand sqlCommand = new SqlCommand("SELECT * FROM Monitor WHERE monitorSerial ='" + serial + "'", sqlConnection);
if (sqlCommand.ExecuteReader().HasRows)
return true;
}
catch (SqlException e)
{
Console.WriteLine("Sql error" + e.Message);
}
finally
{
sqlConnection.Close();
}
return false;
}
}
}