-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHelperFunctions.cs
196 lines (165 loc) · 6.55 KB
/
HelperFunctions.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using DPUruNet;
using System.Drawing.Imaging;
using System.Drawing;
namespace UareUWindowsMSSQLCSharp
{
class HelperFunctions
{
private static Fmd[] fmd1s;
private static Fmd[] fmd2s;
private static Fmd[] allDBFmd1s;
private static Fmd[] allDBFmd2s;
public static Fmd[] GetAllFmd1s
{
get { return allDBFmd1s; }
}
public static Fmd[] GetAllFmd2s
{
get { return allDBFmd2s; }
}
private static string[] allUserNames;
public static string[] GetAllUserNames
{
get { return allUserNames; }
}
private static int[] allfingerIDs;
public static int[] GetallfingerIDs
{
get { return allfingerIDs; }
}
public static SqlDataReader ConnectDBnExecuteSelectScript(string Script)
{
string connectionString = "Data Source=localhost; Initial Catalog=CustomerInfo; Integrated Security=True; Connect Timeout=0; Trusted_Connection=Yes";
//"server=localhost;User Id=root;Persist Security Info=True;database=customerInfo;Password=sql";
SqlDataReader dataReader = null;
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
SqlCommand sqlCommand = new SqlCommand(Script, conn);
dataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (SqlException ex)
{
MessageBox.Show("Error opening connection" + ex.Message);
}
return dataReader;
}
public static void LoadAllUsers()
{
string allUsers = "Select * from users";
LoadAllFmdsUserIDs(allUsers);
}
public static void updateFMDUserIDList(Fmd fmd1, Fmd fmd2, String userid)
{
Array.Resize(ref allUserNames, (allUserNames.Length) + 1);
allUserNames[(allUserNames.Length) - 1] = userid;
Array.Resize(ref allDBFmd1s, (allDBFmd1s.Length) + 1);
allDBFmd1s[(allDBFmd1s.Length) - 1] = fmd1;
Array.Resize(ref allDBFmd2s, (allDBFmd2s.Length) + 1);
allDBFmd2s[(allDBFmd2s.Length) - 1] = fmd2;
}
public static void LoadAllFmdsUserIDs(string SelectQuery)
{
SqlDataReader lReader = ConnectDBnExecuteSelectScript(SelectQuery);
int i = 0;
// populate all fmds and usernames
DataTable dt = new DataTable();
dt.Load(lReader);
allDBFmd1s = new Fmd[dt.Rows.Count];
allDBFmd2s = new Fmd[dt.Rows.Count];
allUserNames = new string[dt.Rows.Count];
// allfingerIDs = new int[dt.Rows.Count];
if (dt.Rows.Count != 0)
{
foreach (System.Data.DataRow dr in dt.Rows)
{
if ((dr["FMD1"].ToString().Length != 0) && (dr["FMD2"].ToString().Length != 0))
{
allDBFmd1s[i] = Fmd.DeserializeXml(dr["FMD1"].ToString());
allDBFmd2s[i] = Fmd.DeserializeXml(dr["FMD2"].ToString());
allUserNames[i] = dr["UserID"].ToString().TrimEnd();
i++;
}
}
}
lReader.Close();
}
//public static Fmd NumberOfRecordsForUserID(string SelectQuery)
//{
// SqlDataReader lReader = ConnectDBnExecuteSelectScript(SelectQuery);
// int i = 0;
// // populate fmds and usernames for a given userid
// DataTable dt = new DataTable();
// dt.Load(lReader);
// if (dt.Rows.Count != 0)
// {
// foreach (System.Data.DataRow dr in dt.Rows)
// {
// if ((dr["FMD1"].ToString().Length != 0) && (dr["FMD2"].ToString().Length != 0))
// {
// fmd1s[i] = Fmd.DeserializeXml(dr["FMD1"].ToString());
// fmd2s[i] = Fmd.DeserializeXml(dr["FMD1"].ToString());
// i++;
// }
// }
// }
// lReader.Close();
// return fmds;
//}
public static void ConnectDBnExecuteScript(string Script)
{
string connectionString = "Data Source=localhost; Initial Catalog=CustomerInfo; Integrated Security=True; Connect Timeout=0; Trusted_Connection=Yes";
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
SqlCommand sqlCommand = new SqlCommand(Script, conn);
sqlCommand.ExecuteScalar();
}
catch (SqlException ex)
{
//If database already has a record for that userid then let customer know that there is a duplicate
if (ex.Message.Contains("Duplicate"))
{
MessageBox.Show("Userid already exists in Database. Please user another UserID");
}
else
MessageBox.Show("Error when connecting to Database" + ex.Message);
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
public static Bitmap CreateBitmap(Byte[] bytes, int width, int height)
{
byte[] rgbBytes = new byte[bytes.Length * 3];
for (int i = 0; i <= bytes.Length - 1; i++)
{
rgbBytes[(i * 3)] = bytes[i];
rgbBytes[(i * 3) + 1] = bytes[i];
rgbBytes[(i * 3) + 2] = bytes[i];
}
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
for (int i = 0; i <= bmp.Height - 1; i++)
{
IntPtr p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i);
System.Runtime.InteropServices.Marshal.Copy(rgbBytes, i * bmp.Width * 3, p, bmp.Width * 3);
}
bmp.UnlockBits(data);
return bmp;
}
}
}