-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataLayer.cs
307 lines (283 loc) · 11.3 KB
/
DataLayer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.IO;
using System.Collections;
using System.Runtime;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Threading;
using System.Windows.Forms;
namespace Quiz
{
static class DataLayer
{
static SQLiteConnection mDBcon = new SQLiteConnection("Data Source = Library\\DB\\memory.db"); //connection string
static SQLiteDataAdapter datadapter;
static SQLiteCommand cmd;
static DataSet dataset = new DataSet();
static Random randmnum = new Random();
/// <summary>
/// This Method Creates the Necessary Tables in the Database if it doesnt Already Exist.
/// </summary>
public static void CreateFile()
{
if (!File.Exists("Library\\DB\\memory.db"))
{
cmd = new SQLiteCommand(mDBcon);
mDBcon.Open();
cmd.CommandText = "CREATE TABLE if not exists Quiz(Id INTEGER PRIMARY KEY AUTOINCREMENT ,Question varchar(1000),type varchar(100),category varchar(100),Image binary,choice1 varchar(100),choice2 varchar(100),choice3 varchar(100),choice4 varchar(100),correctchoice varchar(100))";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE if not exists TypeMaster(Id INTEGER PRIMARY KEY AUTOINCREMENT ,TypeName varchar(1000))";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE if not exists CategoryMaster(Id INTEGER PRIMARY KEY AUTOINCREMENT ,CategoryName varchar(1000))";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE if not exists User(Id INTEGER PRIMARY KEY AUTOINCREMENT ,UserName varchar(1000),Password varchar(1000))";
cmd.ExecuteNonQuery();
mDBcon.Close();
}
}
/// <summary>
/// This Method Retrives Datafrom the Quiz Table.
/// </summary>
/// <param>
/// <c>admin</c>Determines if the call is from admin listing or Quiz to randomize result.
/// </param>
/// <returns>
/// Dataset containing data from the Quiz table.
/// </returns>
public static DataSet DisplayData(bool admin = false)
{
CreateFile();
mDBcon.Open();
DataSet dataset = new DataSet();
cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = admin?"select * from Quiz": "select * from Quiz ORDER BY RANDOM()";//";
datadapter = new SQLiteDataAdapter(cmd);
datadapter.Fill(dataset, "Quiz");
mDBcon.Close();
return dataset;
}
/// <summary>
/// This method retrives data from Typemaster table .
/// </summary>
/// <returns>
/// Dataset containg typemaster data
/// </returns>
public static DataSet GetTypeMaster()
{
CreateFile();
mDBcon.Open();
DataSet dataset = new DataSet();
cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = "select * from TypeMaster";
datadapter = new SQLiteDataAdapter(cmd);
datadapter.Fill(dataset, "TypeMaster");
mDBcon.Close();
return dataset;
}
/// <summary>
/// This method retrives data from CategoryMaster table .
/// </summary>
/// <returns>
/// Dataset containg CategoryMaster data
/// </returns>
public static DataSet GetCategoryMaster()
{
CreateFile();
mDBcon.Open();
DataSet dataset = new DataSet();
cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = "select * from CategoryMaster";
datadapter = new SQLiteDataAdapter(cmd);
datadapter.Fill(dataset, "CategoryMaster");
mDBcon.Close();
return dataset;
}
/// <summary>
/// This method retrives stored image from the database.
/// </summary>
/// <param>
/// <c>Id</c>is the question id of the Image.
/// </param>
/// <returns>
///byte array of the image
/// </returns>
public static byte[] LoadImage(int Id)
{
byte[] a = null;
string query = "SELECT Image FROM Quiz WHERE ID="+ Id +";";
mDBcon.Open();
SQLiteCommand cmd = new SQLiteCommand(query, mDBcon);
try
{
IDataReader rdr = cmd.ExecuteReader();
try
{
while (rdr.Read())
{
a = (System.Byte[])rdr[0];
}
}
catch {
//MessageBox.Show(exc.Message);
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
mDBcon.Close();
return a;
}
/// <summary>
/// This method clears datafrom given table.
/// </summary>
/// <param>
/// <c>tablename</c> is the name of the table.
/// </param>
public static void clearData(string tablename)
{
mDBcon.Open();
var trans = mDBcon.BeginTransaction();
cmd.CommandText = "delete from "+tablename;
cmd.ExecuteNonQuery();
cmd.CommandText = "DELETE FROM SQLITE_SEQUENCE WHERE name='" + tablename +"'";
cmd.ExecuteNonQuery();
trans.Commit();
mDBcon.Close();
}
/// <summary>
/// This method inserts data into the Quiz table .
/// </summary>
/// <param>
/// <c>row</c> is the row to be inserted.
/// </param>
/// <param>
/// <c>image</c> is the Image byte to be inserted.
/// </param>
public static void InsertData(DataGridViewRow row , byte[] image)
{
try
{
mDBcon.Open();
var trans = mDBcon.BeginTransaction();
SQLiteCommand cmd = new SQLiteCommand(mDBcon);
SQLiteParameter A = new SQLiteParameter("@Question", System.Data.DbType.String);
A.Value = row.Cells[1].Value;
cmd.Parameters.Add(A);
SQLiteParameter B = new SQLiteParameter("@type", System.Data.DbType.String);
B.Value = row.Cells[3].Value;
cmd.Parameters.Add(B);
SQLiteParameter C = new SQLiteParameter("@category", System.Data.DbType.String);
C.Value = row.Cells[2].Value;
cmd.Parameters.Add(C);
SQLiteParameter D = new SQLiteParameter("@Image", System.Data.DbType.Binary);
D.Value = image;
cmd.Parameters.Add(D);
SQLiteParameter E = new SQLiteParameter("@choice1", System.Data.DbType.String);
E.Value = row.Cells[5].Value;
cmd.Parameters.Add(E);
SQLiteParameter F = new SQLiteParameter("@choice2", System.Data.DbType.String);
F.Value = row.Cells[6].Value;
cmd.Parameters.Add(F);
SQLiteParameter G = new SQLiteParameter("@choice3", System.Data.DbType.String);
G.Value = row.Cells[7].Value;
cmd.Parameters.Add(G);
SQLiteParameter H = new SQLiteParameter("@choice4", System.Data.DbType.String);
H.Value = row.Cells[8].Value;
cmd.Parameters.Add(H);
SQLiteParameter I = new SQLiteParameter("@correctchoice", System.Data.DbType.String);
I.Value = row.Cells[9].Value;
cmd.Parameters.Add(I);
cmd.CommandText = "INSERT INTO Quiz(Question,type,category,Image,choice1,choice2,choice3,choice4,correctchoice) VALUES(@Question ,@type,@category,@Image,@choice1,@choice2,@choice3,@choice4,@correctchoice)";
cmd.ExecuteNonQuery();
trans.Commit();
mDBcon.Close();
}
catch(Exception ex)
{
}
}
/// <summary>
/// This method validates the user login .
/// </summary>
/// <param>
/// <c>username</c> is the username.
/// </param>
/// /// <param>
/// <c>password</c> is the password.
/// </param>
/// <returns>
/// True if valid False if Invalid.
/// </returns>
public static bool Login(string username ,string password)
{
mDBcon.Open();
DataSet dataset = new DataSet();
cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = "select * from User where username = '"+username+"' and password = '"+password+"'";
datadapter = new SQLiteDataAdapter(cmd);
datadapter.Fill(dataset, "user");
mDBcon.Close();
return dataset.Tables[0].Rows.Count > 0 ?true:false;
}
/// <summary>
/// This method converts the given string to MD5 Hash.
/// </summary>
/// <param>
/// <c>input</c>is the sting to be converted.
/// </param>
/// <returns>
/// MD5 of the input string.
/// </returns>
public static string CreateMD5(string input)
{
// Use input string to calculate MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
/// <summary>
/// This method inserts data into typeMaster table.
/// </summary>
/// <param>
/// <c>row</c>is the row to be inserted.
/// </param>
public static void InsertTypeMasterData(DataGridViewRow row)
{
mDBcon.Open();
var trans = mDBcon.BeginTransaction();
SQLiteCommand cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = "INSERT INTO TypeMaster(TypeName) VALUES('" + row.Cells[1].Value + "')";
cmd.ExecuteNonQuery();
trans.Commit();
mDBcon.Close();
}
/// <summary>
/// This method inserts data into typeMaster table.
/// </summary>
/// <param>
/// <c>row</c>is the row to be inserted.
/// </param>
public static void InsertCategoryMasterData(DataGridViewRow row)
{
mDBcon.Open();
var trans = mDBcon.BeginTransaction();
SQLiteCommand cmd = new SQLiteCommand(mDBcon);
cmd.CommandText = "INSERT INTO CategoryMaster(CategoryName) VALUES('" + row.Cells[1].Value + "')";
cmd.ExecuteNonQuery();
trans.Commit();
mDBcon.Close();
}
}
}