This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
176 lines (149 loc) · 6.12 KB
/
frmMain.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using Laim;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.IO;
namespace SnowDocumentDownloader
{
public partial class frmMain : Form
{
private bool isConnected = false;
private string connectionString = "";
private readonly int CID = 1;
public frmMain()
{
InitializeComponent();
try
{
Directory.CreateDirectory("Data");
Directory.CreateDirectory(@"Data\Agreements");
Directory.CreateDirectory(@"Data\Licenses");
Directory.CreateDirectory(@"Data\Users");
Directory.CreateDirectory(@"Data\Computers");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace);
}
}
private void cbSQLWindowsAuth_CheckedChanged(object sender, EventArgs e)
{
if(cbSQLWindowsAuth.Checked)
{
txtSQLUser.Enabled = false;
txtSQLPass.Enabled = false;
} else
{
txtSQLUser.Enabled = true;
txtSQLPass.Enabled = true;
}
}
private void btnSQLConnect_Click(object sender, EventArgs e)
{
if (txtSQLServer.Text == string.Empty)
{
MessageBox.Show("SQL Server is a required field.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (cbSQLWindowsAuth.Checked)
{
connectionString = "Data Source=" + txtSQLServer.Text + ";Application Name=SLMDocDownloader;Database=SnowLicenseManager;Integrated Security=SSPI";
}
else
{
if (txtSQLUser.Text == string.Empty || txtSQLPass.Text == string.Empty)
{
MessageBox.Show("Username and Password are required if not using Windows Authentication.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
connectionString = "Data Source=" + txtSQLServer.Text + ";Application Name=SLMDocDownloader;Database=SnowLicenseManager;User=" + txtSQLUser.Text + ";Password=" + txtSQLPass.Text;
}
if (connectionString == null)
{
MessageBox.Show("Connection String is not set. This should never happen.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
isConnected = MSSqlServer.CheckConnnection(connectionString);
if (isConnected)
{
gbSQLServer.Enabled = false;
cbDocType.Enabled = true;
btnSaveAttachment.Enabled = true;
cbDocType.SelectedIndex = 0;
}
else
{
MessageBox.Show("Connection was unsuccessful", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cbDocType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string q = "";
switch (cbDocType.Text)
{
case "AGREEMENTS":
q = Properties.Resources.GetDocumentDataAgreements;
break;
case "LICENSES":
q = Properties.Resources.GetDocumentDataLicenses;
break;
case "USERS":
q = Properties.Resources.GetDocumentDataUsers;
break;
case "COMPUTERS":
q = Properties.Resources.GetDocumentDataComputers;
break;
default:
break;
}
List<SqlParameter> p = new List<SqlParameter> { new SqlParameter("@CID", CID) };
dgvDocumentData.DataSource = MSSqlServer.ExecuteReadDataTable(connectionString, q, p);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace);
}
}
private void btnSaveAttachment_Click(object sender, EventArgs e)
{
try
{
if (dgvDocumentData.SelectedRows == null)
{
MessageBox.Show("Select a row from the above grid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
int fileID = (int)dgvDocumentData.SelectedRows[0].Cells["File ID"].Value;
string fileName = (string)dgvDocumentData.SelectedRows[0].Cells["FileName"].Value; ;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(Properties.Resources.GetDocumentContent, conn);
cmd.Parameters.Add("@CID", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@FileID", SqlDbType.Int).Value = fileID;
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
byte[] buffer = dt.AsEnumerable().Select(c => c.Field<byte[]>("Content")).SingleOrDefault();
string filePath = @"Data\" + cbDocType.Text + @"\" + fileName;
File.WriteAllBytes(filePath, buffer);
conn.Close();
if(File.Exists(filePath))
{
MessageBox.Show("Attachment saved.", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace);
}
}
private void btnAbout_Click(object sender, EventArgs e)
{
MessageBox.Show("Copyright (c) Laim McKenzie 2022 - v0.1");
MessageBox.Show("This software is not affiliated with Snow Software.");
MessageBox.Show("https://laim.scot");
}
}
}