-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlController.cs
More file actions
141 lines (117 loc) · 4.02 KB
/
sqlController.cs
File metadata and controls
141 lines (117 loc) · 4.02 KB
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
using PBLDatabaseFrontend.Properties;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace PBLDatabaseFrontend
{
class SQLController
{
SQLiteConnection conn = new SQLiteConnection(@"Data Source=pontybrynlibrary.db");
SQLiteDataAdapter adapter;
/// <summary>
/// Executes Select Statements
/// </summary>
/// <param name="query">The query you want to run</param>
/// <returns>A data table containing the query results</returns>
public DataTable RunQuery(string query)
{
DataTable dt = new DataTable();
try
{
SQLiteCommand cmd;
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = query;
adapter = new SQLiteDataAdapter(cmd);
adapter.Fill(dt);
}
catch(SQLiteException ex)
{
Debug.WriteLine(ex.Message);
}
conn.Close();
return dt;
}
/// <summary>
/// Execute Table functions such as Update or Insert
/// </summary>
/// <param name="nonQuery">The table function statement (i.e update)</param>
/// <param name="parameterMap">The list of parameters using the customer ParameterMap data class (Optional)</param>
/// <returns>The number of records changed</returns>
//public int RunNonQuery(string nonQuery, List<ParameterMap> parameterMap)
//{
// int recordsChanged = 0;
// try
// {
// SQLiteCommand cmd;
// conn.Open();
// cmd = conn.CreateCommand();
// cmd.CommandText=nonQuery;
// foreach(ParameterMap param in parameterMap)
// {
// cmd.Parameters.AddWithValue(param.parameterName, param.parameterValue);
// }
// recordsChanged = cmd.ExecuteNonQuery();
// }
// catch (SQLiteException ex)
// {
// Debug.WriteLine(ex.Message);
// }
// conn.Close();
// return recordsChanged;
//}
public int RunNonQuery(string nonQuery)
{
int recordsChanged = 0;
try
{
SQLiteCommand cmd;
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = nonQuery;
recordsChanged = cmd.ExecuteNonQuery();
}
catch (SQLiteException ex)
{
Debug.WriteLine(ex.Message);
}
conn.Close();
return recordsChanged;
}
/// <summary>
/// Checks if Database exists - if it doesn't, create the database and populate it with default data
/// </summary>
public void InitialiseDatabase()
{
if (!File.Exists("pontybrynlibrary.db"))
{
SQLiteConnection.CreateFile("pontybrynlibrary.db");
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
// This file contains an exported version of the SQL Database off BlackBoard
cmd.CommandText = Resources.pontybrynlibrarySETUP;
cmd.ExecuteNonQuery();
conn.Close();
}
}
/// <summary>
/// Applies the changes made in a given Data Table to the respective SQL Database Table
/// </summary>
/// <param name="dt">The Datatable to copy across</param>
public void CopyDTtoDB(DataTable dt)
{
conn.Open();
using(SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter))
{
adapter.Update(dt);
}
}
}
}