· 8 years ago · Mar 28, 2018, 10:50 AM
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Linq;
5
6using Npgsql;
7using NpgsqlTypes;
8using System.Data;
9using System.Data.SqlClient;
10using System.Data.SqlTypes;
11using System.IO;
12
13namespace Project3
14{
15 class Program
16 {
17 static void Main(string[] args)
18 {
19 GeneralParser parser = new GeneralParser();
20 List<List<String>> School1 = parser.ParseCSV
21 (
22 @"D:\AlleCsv\scholen\school01_Basis.csv",
23 ';',
24 (int line, int column, string value) => { if (column < 5) { return true; } else { return false; }
25 },
26 (string value) => value + " "
27 );
28
29
30 for (int i = 0; i < School1.Count(); i++)
31 {
32 for (int j = 0; j < School1[0].Count(); j++)
33 {
34 Console.Write(School1[i][j]);
35 }
36 Console.WriteLine();
37 }
38 //editData();
39 //readData();
40 Console.Read();
41 }
42
43
44
45 public static DataTable GrabData(string statement)
46 {
47 DataTable DT = new DataTable();
48
49 NpgsqlConnection Con = new NpgsqlConnection("Host=localhost;Username=postgres;Password=test;Database=Test");
50 NpgsqlCommand Comm = new NpgsqlCommand(statement, Con);
51 NpgsqlDataAdapter ada = new NpgsqlDataAdapter(Comm);
52 Con.Open();
53 var cmd = new NpgsqlCommand(statement, Con);
54 var reader = cmd.ExecuteReader();
55 while (reader.Read())
56 {
57 DT.Rows.Add(reader.GetString(0));
58 }
59 Con.Close();
60 return DT;
61 }
62
63 //public static void createTable()
64 //
65 //public static void insertIntoTable()
66 //
67 //public static void readFromTable()
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 public static void readData()
96 {
97 DataSet DS = new DataSet();
98 DataTable DT = new DataTable();
99 NpgsqlConnection Con = new NpgsqlConnection("Host=localhost;Username=postgres;Password=test;Database=Test");
100
101 Con.Open();
102 NpgsqlDataAdapter DA = new NpgsqlDataAdapter("SELECT * FROM lolz", Con);
103 DA.Fill(DS);
104 DT = DS.Tables[0];
105 foreach(DataRow ROW in DT.Rows)
106 {
107 for (int i = 0; i < DT.Columns.Count; i++)
108 {
109 Console.Write(ROW[i] + " ");
110 }
111 Console.Write("\n");
112 }
113
114 Con.Close();
115
116 }
117
118 public static void editData()
119 {
120 NpgsqlConnection Con = new NpgsqlConnection("Host=localhost;Username=postgres;Password=test;Database=Test");
121 NpgsqlCommand Com = new NpgsqlCommand();
122 Com.Connection = Con;
123 Con.Open();
124 Com.CommandText = "DROP TABLE lolz";
125 Com.ExecuteNonQuery();
126 Com.CommandText = @"CREATE TABLE if not exists lolz
127(
128 id SERIAL,
129 name varchar(30)
130); ";
131 Com.ExecuteNonQuery();
132 Random rnd = new Random();
133 for (int i = 0; i < 100; i++)
134 {
135 int a = rnd.Next(1, 6);
136 switch(a)
137 {
138 case 1:
139 Com.CommandText = "INSERT INTO lolz(name) VALUES ('FRED');";
140 break;
141 case 2:
142 Com.CommandText = "INSERT INTO lolz(name) VALUES ('EDDY');";
143 break;
144 case 3:
145 Com.CommandText = "INSERT INTO lolz(name) VALUES ('HERBERT');";
146 break;
147 case 4:
148 Com.CommandText = "INSERT INTO lolz(name) VALUES ('HENDRIK');";
149 break;
150 case 5:
151 Com.CommandText = "INSERT INTO lolz(name) VALUES ('BOB');";
152 break;
153
154 }
155 Com.ExecuteNonQuery();
156 }
157
158 Con.Close();
159 }
160 }
161
162 public class GeneralParser
163 {
164 public List<List<string>> ParseCSV
165 (
166 string location,
167 char seperator,
168 Func<int, int, string, bool> filter,
169 Func<string, string> action
170 )
171 {
172 List<List<string>> parsed = new List<List<string>>();
173 try
174 {
175 using (StreamReader reader = new StreamReader(location))
176 {
177 string line;
178 /* Lees elke regel van het csv bestand */
179 int lineCounter = 0;
180 while ((line = reader.ReadLine()) != null)
181 {
182 lineCounter += 1;
183 /* Splits de regel op in kolommen en loop door elke kolom heen */
184 string[] columns = line.Split(seperator);
185 int columnCounter = 0;
186 List<string> rowList = new List<string>();
187 foreach (string column in columns)
188 {
189 columnCounter += 1;
190 /* Filter */
191 if (filter(lineCounter, columnCounter, column))
192 {
193 /* Voer een actie uit op de waarde en voeg het toe aan de rijlijst */
194 rowList.Add(action(column));
195 }
196 }
197 parsed.Add(rowList);
198 }
199 }
200
201 }
202 catch (Exception e)
203 {
204 Console.WriteLine("Something went wrong while reading the csv file: " + e);
205 }
206 return parsed;
207 }
208
209 public List<List<string>> parseList
210 (
211 List<List<string>> list,
212 Func<List<List<string>>, List<string>, bool> filter,
213 Func<List<List<string>>, List<string>, List<string>> action
214 )
215 {
216 List<List<string>> parsed = new List<List<string>>();
217 foreach (List<string> sublist in list)
218 {
219 if (filter(list, sublist))
220 parsed.Add(action(list, sublist));
221 }
222 return parsed;
223 }
224
225 public List<string> parseList
226 (
227 List<string> list,
228 Func<List<string>, string, bool> filter,
229 Func<List<string>, string, string> action
230 )
231 {
232 List<string> parsed = new List<string>();
233 foreach (string value in list)
234 {
235 if (filter(list, value))
236 parsed.Add(action(list, value));
237 }
238 return parsed;
239 }
240
241 public List<string> compareLists(List<string> list1, List<string> list2)
242 {
243 List<string> compared = new List<string>();
244 IEnumerable<string> intersection = list1.Intersect(list2);
245 foreach (string value in intersection)
246 {
247 compared.Add(value);
248 }
249 return compared;
250 }
251
252 public List<List<string>> compareLists(List<List<string>> list1, List<List<string>> list2)
253 {
254 List<List<string>> compared = new List<List<string>>();
255 parseList(list1, (listOne, value) => true, (listOne, value) => {
256 parseList(list2, (listTwo, value2) => true, (listTwo, value2) => {
257 return compareLists(value, value2);
258 });
259 return value;
260 });
261 return compared;
262 }
263
264 public List<string> ListTo1d(List<List<string>> list, Func<int, string, bool> filter)
265 {
266 List<string> result = new List<string>();
267 /* Loop door de eerste laag */
268 foreach (List<string> sublist in list)
269 {
270 int counter = 0;
271 /* Loop door de tweede laag */
272 foreach (string value in sublist)
273 {
274 counter += 1;
275 /* Filter */
276 if (filter(counter, value))
277 {
278 result.Add(value);
279 }
280 }
281 }
282 return result;
283 }
284 }
285
286 public class timsSQLConnectionThing
287 {
288
289 }
290}