-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataProvider.cs
324 lines (279 loc) · 10.6 KB
/
DataProvider.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;
using System.Reflection;
using System.Data;
namespace BTL_HQTCSDL
{
class DataProvider
{
//private string connStr= ("Server=127.0.0.1;Port=5432;User Id=postgres;Password=56tyghbn;Database=gis;");
NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=56tyghbn;Database=gisdb;");
#region CatchException
private void OpenConn()
{
try
{
conn.Open();
}
catch (Exception exp)
{
MessageBox.Show("Error :S");
}
}
private void CloseConn()
{
try
{
conn.Close();
}
catch (Exception)
{
MessageBox.Show("Error :S");
}
}
#endregion
public DataTable ExecuteQuery(string query)
{
this.OpenConn();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
//using (NpgsqlConnection conn = new NpgsqlConnection(connStr))
//this.OpenConn();
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
NpgsqlDataAdapter adap = new NpgsqlDataAdapter(cmd);
ds.Reset();
adap.Fill(ds);
//dt = ds.Tables[0];
CloseConn();
return dt;
}
public void StoreImage(string layerName, string featureID, byte[] xCode)
{
this.OpenConn();
DataSet ds = new DataSet();
string query = string.Format("UPDATE {0} SET image =@Re WHERE id ='{1}'", layerName, featureID);
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
cmd.CommandText = query;
cmd.Parameters.Add("Re", xCode);
cmd.ExecuteNonQuery();
//ds.Reset();
//adap.Fill(ds);
//dt = ds.Tables[0];
CloseConn();
MessageBox.Show("Done");
}
public byte[] GetImage(string layerName, string featureID)
{
this.OpenConn();
string query = string.Format("select image from {0} WHERE id = {1}",layerName,featureID);
var cmd = new NpgsqlCommand(query, conn);
var reader = cmd.ExecuteReader();
reader.Read();
var xCode = (byte[])reader[0];
byte[] xByte = xCode;
CloseConn();
return xByte;
}
public void InsertOnTable(Object objGen)
{
try
{
this.OpenConn();
// Get type and properties (vector)
Type typeObj = objGen.GetType();
PropertyInfo[] properties = typeObj.GetProperties();
// Get table
string[] type = typeObj.ToString().Split('.');
string table = type[2].ToLower();
// Start mounting string to insert
string SQL = "INSERT INTO " + table + " VALUES (";
// It goes from second until second to last
for (int i = 1; i < properties.Length - 1; i++)
{
object propValue = properties[i].GetValue(objGen, null);
string[] typeValue = propValue.GetType().ToString().Split('.');
if (typeValue[1].Equals("String"))
{
SQL += "'" + propValue.ToString() + "',";
}
else if (typeValue[1].Equals("DateTime"))
{
SQL += "'" + Convert.ToDateTime(propValue.ToString()).ToShortDateString() + "',";
}
else
{
SQL += propValue.ToString() + ",";
}
}
// get last attribute here
object lastValue = properties[properties.Length - 1].GetValue(objGen, null);
string[] lastType = lastValue.GetType().ToString().Split('.');
if (lastType[1].Equals("String"))
{
SQL += "'" + lastValue.ToString() + "'";
}
else if (lastType[1].Equals("DateTime"))
{
SQL += "'" + Convert.ToDateTime(lastValue.ToString()).ToShortDateString() + "'";
}
else
{
SQL += lastValue.ToString();
}
// Ends string builder
SQL += ");";
// Execute command
NpgsqlCommand command = new NpgsqlCommand(SQL, conn);
Int32 rowsaffected = command.ExecuteNonQuery();
this.CloseConn();
}
catch (Exception)
{
MessageBox.Show("Errr on insert!");
}
}
public void UpdateOnTable(Object objGen, int idValue)
{
try
{
this.OpenConn();
// Get table
string[] type = objGen.GetType().ToString().Split('.');
string table = type[2].ToLower();
// Start building
string SQL = "UPDATE " + table + " SET ";
// Get types and properties
Type type2 = objGen.GetType();
PropertyInfo[] properties = type2.GetProperties();
// Goes until second to last
for (int i = 0; i < properties.Length - 1; i++)
{
object propValue = properties[i].GetValue(objGen, null);
string[] nameAttribute = properties[i].ToString().Split(' ');
string[] typeAttribute = propValue.GetType().ToString().Split('.');
if (typeAttribute[1].Equals("String"))
{
SQL += nameAttribute[1] + " = '" + propValue.ToString() + "',";
}
else if (typeAttribute[1].Equals("DateTime"))
{
SQL += nameAttribute[1] + "= '" + Convert.ToDateTime(propValue.ToString()).ToShortDateString() + "',";
}
else
{
SQL += nameAttribute[1] + " = " + propValue.ToString() + ",";
}
}
// Process last attribute
object lastValue = properties[properties.Length - 1].GetValue(objGen, null);
string[] lastType = lastValue.GetType().ToString().Split('.');
string[] ultimoCampo = properties[properties.Length - 1].ToString().Split(' ');
if (lastType[1].Equals("String"))
{
SQL += ultimoCampo[1] + " = '" + lastValue.ToString() + "'";
}
else if (lastType[1].Equals("DateTime"))
{
SQL += ultimoCampo[1] + "= '" + Convert.ToDateTime(lastValue.ToString()).ToShortDateString() + "'";
}
else
{
SQL += ultimoCampo[1] + " = " + lastValue.ToString();
}
// Ends string builder
SQL += " WHERE id = " + idValue + ";";
// Execute query
NpgsqlCommand command = new NpgsqlCommand(SQL, conn);
Int32 rowsaffected = command.ExecuteNonQuery();
this.CloseConn();
}
catch (Exception)
{
MessageBox.Show("Errr on update!");
}
}
public void DeleteOnTable(Object objGen, int idValue)
{
try
{
this.OpenConn();
string[] type = objGen.GetType().ToString().Split('.');
string table = type[2];
string SQL = "DELETE FROM " + table + " WHERE id = " + idValue + ";";
NpgsqlCommand command = new NpgsqlCommand(SQL, conn);
Int32 rowsaffected = command.ExecuteNonQuery();
this.CloseConn();
}
catch (Exception)
{
MessageBox.Show("Errr on update!");
}
}
public List<Object> QueryAllOnTable(string table)
{
try
{
this.OpenConn();
List<Object> lstSelect = new List<Object>();
string SQL = "SELECT * FROM " + table + ";";
NpgsqlCommand command = new NpgsqlCommand(SQL, conn);
NpgsqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
lstSelect.Add(dr[i]);
}
}
this.CloseConn();
return lstSelect;
}
catch (Exception)
{
MessageBox.Show("Errr on query!");
return null;
}
}
//public List<Object> QueryOnTableWithParams(string table, string[] paramName, string[] paramValue)
//{
// try
// {
// if (paramName.Count != paramValue.Count)
// {
// MessageBox.Show("Wrong number of params");
// return null;
// }
// this.OpenConn();
// List<Object> lstSelect = new List<Object>();
// string SQL = "SELECT * FROM " + table + " WHERE ";
// // get params
// for (int i = 0; i < paramName.Count - 1; i++)
// {
// SQL += paramName[i] + " = " + paramValue[i] + " AND ";
// }
// // get last param
// SQL += paramName[paramName.Count - 1] + " = " + paramValue[paramValue.Count - 1] + ";";
// NpgsqlCommand command = new NpgsqlCommand(SQL, conn);
// NpgsqlDataReader dr = command.ExecuteReader();
// while (dr.Read())
// {
// for (int i = 0; i < dr.FieldCount; i++)
// {
// lstSelect.Add(dr[i]);
// }
// }
// this.CloseConn();
// return lstSelect;
// }
// catch (Exception)
// {
// MessageBox.Show("Errr on query!");
// return null;
// }
//}
}
}