Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using Npgsql;
- using System.Data;
- using System.Text.RegularExpressions;
- namespace Project3
- {
- class Program
- {
- static void Main(string[] args)
- {
- GeneralParser parser = new GeneralParser();
- string[] ScholenNaamTypes = { "id", "instellingsnaam", "straatnaam", "wijk"};
- SQLConnectionTool N1 = new SQLConnectionTool("Host = localhost; Username = postgres; Password = Test; Database = Project3Test");
- List<int> straatnamen = new List<int>();
- #region scholen
- //Scholen table
- //0. Brin nummer (PK) (varchar) 2/2/4/3
- //1. Instellingsnaam (varchar) 3/3/5/4
- //2. Straatnaam (integer) 4/4/6/5 (NEEDS CONVERSION)
- //3. Wijk (varchar) 6/6/8/7 (NEEDS CONVERSION)
- //4. Niveau (varchar) x/x/x/x
- //5. Plaatsnaam 7/7/9/8
- List<List<String>> School1 = parser.ParseCSV
- (
- @"D:\AlleCsv\scholen\school01_Basis.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 2 || column == 3 || column == 4 || column == 6 || column == 7) { return true; } else { return false; }
- },
- (string value) => value + " "
- );
- List<List<String>> School2 = parser.ParseCSV
- (
- @"D:\AlleCsv\scholen\school02_Middelbaar.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 2 || column == 3 || column == 4 || column == 6 || column == 7) { return true; } else { return false; }
- },
- (string value) => value + " "
- );
- List<List<String>> School3 = parser.ParseCSV
- (
- @"D:\AlleCsv\scholen\school03_Mbo.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 4 || column == 5 || column == 6 || column == 8 || column == 9) { return true; } else { return false; }
- },
- (string value) => value + " "
- );
- List<List<String>> School4 = parser.ParseCSV
- (
- @"D:\AlleCsv\scholen\school04_HboWo.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 3 || column == 4 || column == 5 || column == 7 || column == 8) { return true; } else { return false; }
- },
- (string value) => value + " "
- );
- List<List<String>> postcodesenwijken = parser.ParseCSV
- (
- @"D:\AlleCsv\wijken\Postcodesenwijken.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 0 || column == 3) { return true; } else { return false; }
- },
- (string value) => value
- );
- N1.Postcodesenwijken = postcodesenwijken;
- string[] typenamespostcodes = { "Varchar(10)", "Varchar(40)" };
- string[] typenames = { "varchar(10)", "varchar(150)", "varchar(100)", "varchar(100)" };
- List<List<string>> nschool1 = new List<List<string>>();
- for (int i = 0; i < School1.Count; i++)
- {
- if (School1[i][4].ToString().Contains("ROTTERDAM"))
- {
- List<string> l = new List<string>();
- School1[i][2] = N1.straatnaamtoint(School1[i][2], School1[i][3]).ToString();
- School1[i][3] = N1.PostcodeNaarWijk(School1[i][3].Replace(" ", ""));
- for (int j = 0; j < School1[i].Count - 1; j++)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("SCHOOL: " + School1[0][j] + " " + School1[i][j].ToString() + " " + j.ToString());
- l.Add(School1[i][j]);
- }
- nschool1.Add(l);
- }
- }
- N1.insertIntoTable("scholen", ScholenNaamTypes.ToList(), nschool1, "niveau", "'lager'");
- List<List<string>> nschool2 = new List<List<string>>();
- for (int i = 0; i < School2.Count; i++)
- {
- if (School2[i][4].ToString().Contains("ROTTERDAM"))
- {
- List<string> l = new List<string>();
- School2[i][2] = N1.straatnaamtoint(School2[i][2], School2[i][3]).ToString();
- School2[i][3] = N1.PostcodeNaarWijk(School2[i][3].Replace(" ", ""));
- for (int j = 0; j < School2[i].Count - 1; j++)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("SCHOOL: " + School2[0][j] + " " + School2[i][j].ToString() + " " + j.ToString());
- l.Add(School2[i][j]);
- }
- nschool2.Add(l);
- }
- }
- N1.insertIntoTable("scholen", ScholenNaamTypes.ToList(), nschool2, "niveau", "'middelbaar'");
- List<List<string>> nschool3 = new List<List<string>>();
- for (int i = 0; i < School3.Count; i++)
- {
- if (School3[i][4].ToString().Contains("ROTTERDAM"))
- {
- List<string> l = new List<string>();
- School3[i][2] = N1.straatnaamtoint(School3[i][2], School3[i][3]).ToString();
- School3[i][3] = N1.PostcodeNaarWijk(School3[i][3].Replace(" ", ""));
- for (int j = 0; j < School3[i].Count - 1; j++)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("SCHOOL: " + School3[0][j] + " " + School3[i][j].ToString() + " " + j.ToString());
- l.Add(School3[i][j]);
- }
- nschool3.Add(l);
- }
- }
- N1.insertIntoTable("scholen", ScholenNaamTypes.ToList(), nschool3, "niveau", "'hoger'");
- List<List<string>> nschool4 = new List<List<string>>();
- for (int i = 0; i < School4.Count; i++)
- {
- if (School4[i][4].ToString().Contains("ROTTERDAM"))
- {
- List<string> l = new List<string>();
- School4[i][2] = N1.straatnaamtoint(School4[i][2], School4[i][3]).ToString();
- School4[i][3] = N1.PostcodeNaarWijk(School4[i][3].Replace(" ", ""));
- for (int j = 0; j < School4[i].Count - 1; j++)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("SCHOOL: " + School4[0][j] + " " + School4[i][j].ToString() + " " + j.ToString());
- l.Add(School4[i][j]);
- }
- nschool4.Add(l);
- }
- }
- N1.insertIntoTable("scholen", ScholenNaamTypes.ToList(), nschool4, "niveau", "'hoger'");
- #endregion
- #region misdaden
- /*create table if not exists misdaad(
- casenummer char(10) primary key,
- einddatumtijd date,
- begindatumtijd date,
- type varchar(20),
- straatnaam int,
- wijk varchar(30),
- jaartal date,
- foreign key (jaartal) references jaartal(jaartal),
- constraint fk_misdaad_straatnaam foreign key (straatnaam, wijk) references straatnaam(id, wijk),
- constraint typecontrole check (type='straatroof' or type='fietsdiefstal')
- */
- //Misdaden table
- //0. Casenummer (PK) (varchar) 00
- //1. Begindatumtijd (datetime) 08,09 (NEEDS CONVERSION)
- //2. Einddatumtijd (datetime) 10,11 (NEEDS CONVERSION)
- //3. Type (varchar) 13
- //4. Straatnaam (int) 19 (NEEDS CONVERSION)
- //5. Wijk (varchar) 17
- //6. Jaartal (varchar) x
- string[] misdaadtypesarray = {"casenummer", "begindatumtijd", "einddatumtijd", "type", "straatnaam", "wijk", "jaartal"};
- List<string> misdaadtypes = misdaadtypesarray.ToList();
- List<List<String>> misdaden = parser.ParseCSV
- (
- @"D:\AlleCsv\misdaden\Straatroof-2011.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 0 || column == 8 || column == 9 || column == 10 || column == 11 || column == 13 || column == 19 || column == 22) { return true; } else { return false; }
- },
- (string value) => value
- );
- List<List<String>> misdaden2 = new List<List<string>>();
- for (int i = 0; i < misdaden.Count; i++)
- {
- List<String> l = new List<string>();
- for (int j = 0; j < misdaden[0].Count; j++)
- {
- try
- {
- Console.ForegroundColor = ConsoleColor.White;
- l.Add(misdaden[i][j]);
- }
- catch
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("NOT ADDED");
- break;
- }
- if (l.Count == misdaden[0].Count)
- {
- misdaden2.Add(l);
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("ADDED");
- }
- }
- Console.WriteLine();
- }
- Console.ForegroundColor = ConsoleColor.White;
- for (int i = 0; i < misdaden2.Count; i++)
- {
- for (int j = 0; j < misdaden2[0].Count; j++)
- {
- Console.Write(misdaden2[i][j] + ", ");
- }
- Console.WriteLine();
- }
- List<List<string>> misdaden3 = new List<List<string>>();
- for (int i = 0; i < misdaden2.Count; i++)
- {
- //0. Casenummer (PK) (varchar) 00
- //1. Begindatumtijd (datetime) 08,09 (NEEDS CONVERSION)
- //2. Einddatumtijd (datetime) 10,11 (NEEDS CONVERSION)
- //3. Type (varchar) 13
- //4. Straatnaam (int) 19 (NEEDS CONVERSION)
- //5. Wijk (varchar) 17
- //6. Jaartal (varchar) x
- if (misdaden2[i][7].Replace(" ", "") != "")
- {
- List<string> l = new List<string>();
- Console.ForegroundColor = ConsoleColor.White;
- l.Add(misdaden2[i][0]);
- l.Add(dateANDtimeTOdatetime(misdaden2[i][1], misdaden2[i][2]));
- l.Add(dateANDtimeTOdatetime(misdaden2[i][3], misdaden2[i][4]));
- l.Add(misdaden2[i][5]);
- l.Add(N1.straatnaamtoint(misdaden2[i][6], misdaden2[i][7]).ToString());
- l.Add(N1.PostcodeNaarWijk(misdaden2[i][7]));
- l.Add("2011");
- misdaden3.Add(l);
- }
- }
- for (int i = 0; i < misdaden3.Count; i++)
- {
- for (int j = 0; j < misdaden3[i].Count; j++)
- {
- Console.WriteLine(misdaadtypes[j] + ": " + misdaden3[i][j]);
- }
- }
- N1.insertIntoTable("misdaad", misdaadtypes, misdaden3, "", "");
- //Misdaden table
- //0. Casenummer (PK) (varchar) 00
- //1. Begindatumtijd (datetime) 11,12 (NEEDS CONVERSION)
- //2. Einddatumtijd (datetime) 14,15 (NEEDS CONVERSION)
- //3. Type (varchar) 3
- //4. Straatnaam (int) 9 (NEEDS CONVERSION)
- //5. Wijk (varchar) 8 (NEEDS CONVERSION)
- //6. Jaartal (varchar) 16
- List<List<String>> fietsendiefstallen1 = parser.ParseCSV
- (
- @"D:\AlleCsv\misdaden\fietsdiefstal-2011-2013.csv",
- ';',
- (int line, int column, string value, string[] columns) =>
- {
- if (column == 0 || column == 11 || column == 12 || column == 14 || column == 15 || column == 3 || column == 9 || column == 8 || column == 16 || column == 7) { return true; } else { return false; }
- },
- (string value) => value
- );
- for (int i = 0; i < fietsendiefstallen1.Count; i++)
- {
- for (int j = 0; j < fietsendiefstallen1[i].Count; j++)
- {
- Console.Write(fietsendiefstallen1[i][j] + ", ");
- }
- Console.WriteLine();
- }
- List<List<string>> fietsendiefstallen2 = new List<List<string>>();
- for (int i = 0; i < fietsendiefstallen1.Count; i++)
- {
- try
- {
- if (fietsendiefstallen1[i][2].Contains("ROTTER"))
- {
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("Fietsendiefstal");
- List<string> l = new List<string>();
- l.Add(fietsendiefstallen1[i][0]);
- l.Add(dateANDtimeTOdatetime(fietsendiefstallen1[i][5], fietsendiefstallen1[i][6]));
- l.Add(dateANDtimeTOdatetime(fietsendiefstallen1[i][7], fietsendiefstallen1[i][8]));
- l.Add("fietsdiefstal");
- Console.WriteLine((Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", "")).ToString());
- Console.WriteLine(N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", "")).ToString());
- if (N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", "")) != 0)
- {
- l.Add(N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", "")).ToString());
- Console.WriteLine(N1.findWijk(N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", ""))));
- if (N1.findWijk(N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", ""))) != "")
- {
- Console.WriteLine("KJ");
- }
- }
- if (N1.findWijk(N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", ""))) != "")
- {
- l.Add(N1.findWijk(N1.straatnaamtoint2(Regex.Replace(fietsendiefstallen1[i][4], @"\d", "").Replace(" ", ""))));
- }
- l.Add(fietsendiefstallen1[i][9]);
- for (int j = 0; j < l.Count; j++)
- {
- Console.Write(l[j].ToString() + ", ");
- }
- Console.WriteLine(l.Count);
- if (l.Count == 7)
- {
- fietsendiefstallen2.Add(l);
- }
- }
- }
- catch
- {
- }
- }
- Console.Write("KJKJ");
- for (int i = 0; i < fietsendiefstallen2.Count; i++)
- {
- for (int j = 0; j < fietsendiefstallen2[i].Count; j++)
- {
- Console.WriteLine(fietsendiefstallen2[i][j] + ": " + misdaadtypes[j]);
- }
- Console.WriteLine();
- }
- N1.insertIntoTable("misdaad", misdaadtypes, fietsendiefstallen2, "", "");
- Console.Read();
- #endregion
- }
- public static string dateANDtimeTOdatetime(string date, string time)
- {
- return date + " " + time;
- }
- }
- //Created by Allon
- public class GeneralParser
- {
- public List<List<string>> ParseCSV(string location, char seperator, Func<int, int, string, string[], bool> filter, Func<string, string> action)
- {
- List<List<string>> parsed = new List<List<string>>();
- try
- {
- using (StreamReader reader = new StreamReader(location))
- {
- string line;
- /* Lees elke regel van het csv bestand */
- int lineCounter = 0;
- while ((line = reader.ReadLine()) != null)
- {
- /* Splits de regel op in kolommen en loop door elke kolom heen */
- string[] columns = line.Split(seperator);
- int columnCounter = 0;
- List<string> rowList = new List<string>();
- foreach (string column in columns)
- {
- /* Filter */
- if (filter(lineCounter, columnCounter, column, columns))
- {
- /* Voer een actie uit op de waarde en voeg het toe aan de rijlijst */
- rowList.Add(action(column));
- }
- columnCounter += 1;
- }
- if (rowList.Count > 0)
- parsed.Add(rowList);
- lineCounter += 1;
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Something went wrong while reading the csv file: " + e);
- }
- return parsed;
- }
- public List<List<string>> parseList(List<List<string>> list, Func<List<List<string>>, List<string>, bool> filter, Func<List<List<string>>, List<string>, List<string>> action)
- {
- List<List<string>> parsed = new List<List<string>>();
- foreach (List<string> sublist in list)
- {
- if (filter(list, sublist))
- if (sublist.Count > 0)
- parsed.Add(action(list, sublist));
- }
- return parsed;
- }
- public List<string> parseList(List<string> list, Func<List<string>, string, bool> filter, Func<List<string>, string, string> action)
- {
- List<string> parsed = new List<string>();
- foreach (string value in list)
- {
- if (filter(list, value))
- if (value != "")
- parsed.Add(action(list, value));
- }
- return parsed;
- }
- public List<List<string>> compareLists(List<string> list1, List<string> list2, Func<List<string>, string, Func<List<string>, string, bool>> filter, Func<List<string>, string, Func<List<string>, string, string>> action)
- {
- List<List<string>> compared = new List<List<string>>();
- compared.Add(parseList(list1, (list, value) => true, (list, value) => {
- compared.Add(parseList(list2, filter(list, value), action(list, value)));
- return value;
- }));
- return compared;
- }
- public List<List<string>> compareLists(List<List<string>> list1, List<List<string>> list2, Func<List<string>, string, Func<List<string>, string, bool>> filter, Func<List<string>, string, Func<List<string>, string, string>> action)
- {
- List<List<string>> compared = new List<List<string>>();
- parseList(list1, (list, sublist) => true, (list, sublist) => {
- return parseList(sublist, (list1Sublist, list1Value) => true, (list1Sublist, list1Value) => {
- parseList(list2, (list2List, list2Sublist) => true, (list2List, list2Sublist) => {
- compared.Add(parseList(list2Sublist, filter(list1Sublist, list1Value), action(list1Sublist, list1Value)));
- return list2Sublist;
- });
- return list1Value;
- });
- });
- return compared;
- }
- public List<string> ListTo1d(List<List<string>> list, Func<int, string, bool> filter)
- {
- List<string> result = new List<string>();
- /* Loop door de eerste laag */
- foreach (List<string> sublist in list)
- {
- int counter = 0;
- /* Loop door de tweede laag */
- foreach (string value in sublist)
- {
- /* Filter */
- if (filter(counter, value))
- {
- result.Add(value);
- }
- counter += 1;
- }
- }
- return result;
- }
- public List<List<string>> removeDuplicates(List<List<string>> list)
- {
- List<List<string>> filtered = new List<List<string>>();
- return filtered;
- }
- public string BuurtNaarWijk(string buurt)
- {
- var buurtenlijst = ParseCSV(@"D:\AlleCsv\wijken", ';', (line, row, column, columns) => columns[1] == buurt && row == 0, (column) => column);
- return buurtenlijst[0][0].Substring(0, 5);
- }
- public string PostcodeNaarWijk(string postcode)
- {
- var postcodeWijkLijst = ParseCSV(@"D:\AlleCsv\wijken\Postcodeseenwijken.csv", ';', (line, row, column, columns) => line > 0 && columns[0].Substring(0, 4) == postcode && row == 3, (column) => column);
- return postcodeWijkLijst[0][0];
- }
- }
- //Created by Tim
- public class SQLConnectionTool
- {
- public List<List<string>> Postcodesenwijken;
- public SQLConnectionTool(string connectionstring)
- {
- CON = new NpgsqlConnection(connectionstring);
- }
- NpgsqlConnection CON = new NpgsqlConnection("Host = localhost; Username = postgres; Password = Test; Database = Project3Test");
- public static List<straatnamen> allestraten = new List<straatnamen>();
- //Voorbeeld input:
- //tablename: "Mijntable"
- //names = {"naam", "leeftijd"}
- //typenames = {"Varchar(30)", "integer"}
- //droptable = true
- //PrimaryKey = null
- //Voorbeeld output:
- //DROP TABLE Mijntable;
- //CREATE TABLE if not exists Mijntable
- //(
- // id SERIAL PRIMARY KEY,
- // naam varchar(30),
- // leeftijd integer
- //);
- public void createTable(string tablename, List<string> names, List<string> typenames, bool dropTable, int PrimaryKey, string extra)
- {
- if (names.Count != typenames.Count)
- {
- //MessageBox.Show("Please make sure that the amount of names equals the amount of type names");
- throw new Exception("YA DUN! Names: " + names.Count + " typenames: " + typenames.Count);
- }
- if (PrimaryKey >= names.Count && PrimaryKey != 0)
- {
- //MessageBox.Show("Please make sure that the primary key is smaller than the amount of data types");
- throw new Exception("YA DUN!");
- }
- NpgsqlCommand COM = new NpgsqlCommand();
- COM.Connection = CON;
- CON.Open();
- if (dropTable)
- {
- COM.CommandText = "DROP TABLE if exists " + tablename + ";";
- Console.Write(COM.CommandText + "\n");
- COM.ExecuteNonQuery();
- }
- COM.CommandText = "CREATE TABLE if not exists " + tablename + "\n ( \n ";
- if (PrimaryKey == 0)
- {
- COM.CommandText = COM.CommandText + "id SERIAL PRIMARY KEY, \n ";
- }
- for (int i = 0; i < typenames.Count; i++)
- {
- COM.CommandText = COM.CommandText + names[i].Replace(" ", "").Replace("-", "_") + " " + typenames[i];
- if (i + 1 == PrimaryKey)
- {
- COM.CommandText = COM.CommandText + " PRIMARY KEY";
- }
- if ((i != typenames.Count && extra != "") || (i != typenames.Count - 1 && extra == ""))
- {
- COM.CommandText = COM.CommandText + ", ";
- }
- COM.CommandText = COM.CommandText + "\n ";
- }
- if (extra != "")
- {
- COM.CommandText = COM.CommandText + extra + " varchar (100)";
- }
- COM.CommandText = COM.CommandText + "\n );\n";
- Console.Write(COM.CommandText);
- COM.ExecuteNonQuery();
- CON.Close();
- }
- //Voorbeeld input:
- //tablename = "Mijntable"
- //names = {"naam", "leeftijd"}
- //typenames = {"Varchar(30)", "integer"}
- //Values = {"Eddy", "42"},{"Fred", "28"}, {"Jos", "6"}, {"Loubna", "6"}, {"Rini", "6"}}
- //
- //Voorbeeld output:
- //INSERT INTO Mijntable (naam, leeftijd) VALUES ('Eddy', 42);
- //INSERT INTO Mijntable (naam, leeftijd) VALUES ('Fred', 28);
- //INSERT INTO Mijntable (naam, leeftijd) VALUES ('Jos', 6);
- //INSERT INTO Mijntable (naam, leeftijd) VALUES ('Loubna', 6);
- //INSERT INTO Mijntable (naam, leeftijd) VALUES ('Rini', 6);
- public void insertIntoTable(string tablename, List<string> names, List<List<string>> Values, string extraName, string extraVal)
- {
- if (names.Count != Values[0].Count)
- {
- //MessageBox.Show("Please make sure that the amount of typenames, names and values are equal");
- throw new Exception("YA DUN! Names: " + names.Count + " values[0]: " + Values[0].Count);
- }
- NpgsqlCommand COM = new NpgsqlCommand();
- COM.Connection = CON;
- CON.Open();
- for (int i = 1; i < Values.Count; i++)
- {
- COM.CommandText = "INSERT INTO " + tablename + "(";
- for (int j = 0; j < Values[i].Count; j++)
- {
- COM.CommandText = COM.CommandText + names[j].Replace(" ", "");
- if ((j != Values[i].Count && extraName != "") || (j != Values[i].Count - 1 && extraName == ""))
- {
- COM.CommandText = COM.CommandText + ", ";
- }
- }
- if (extraName != "")
- {
- COM.CommandText = COM.CommandText + extraName;
- }
- COM.CommandText = COM.CommandText + ") VALUES (";
- for (int j = 0; j < Values[i].Count; j++)
- {
- COM.CommandText = COM.CommandText + "'" + Values[i][j] + "'";
- if ((j != Values[i].Count && extraName != "") || (j != Values[i].Count - 1 && extraName == ""))
- {
- COM.CommandText = COM.CommandText + ", ";
- }
- }
- if (extraName != "")
- {
- COM.CommandText = COM.CommandText + extraVal;
- }
- COM.CommandText = COM.CommandText + "); \n";
- Console.Write(COM.CommandText);
- try
- {
- COM.ExecuteNonQuery();
- }
- catch
- {
- COM.CommandText = "";
- }
- }
- CON.Close();
- }
- public void joinTables(List<List<string>> table1, List<List<string>> table2, string tablename1, string tablename2, string newtable, int joinRowTable1, int joinRowTable2, List<int> selectRowsTable1, List<int> selectRowsTable2)
- {
- NpgsqlCommand COM = new NpgsqlCommand();
- CON.Open();
- COM.CommandText = "SELECT ";
- foreach (int i in selectRowsTable1)
- {
- COM.CommandText = COM.CommandText + tablename1 + "." + table1[0][selectRowsTable1[i]] + ", ";
- }
- int j = 0;
- foreach (int i in selectRowsTable2)
- {
- COM.CommandText = COM.CommandText + tablename2 + "." + table2[0][selectRowsTable2[i]];
- if (j != selectRowsTable2.Count - 1)
- {
- COM.CommandText = COM.CommandText + ", ";
- }
- j++;
- }
- COM.CommandText = COM.CommandText + "\n";
- COM.CommandText = COM.CommandText + "INTO " + newtable + "\n";
- COM.CommandText = COM.CommandText + "FROM " + tablename1 + "\n";
- COM.CommandText = COM.CommandText + "INNER JOIN " + tablename2 + " ON " + tablename1 + "." + table1[0][joinRowTable1] + " = " + tablename2 + "." + table2[0][joinRowTable2] + ";";
- Console.WriteLine(COM.CommandText);
- Console.Read();
- COM.ExecuteNonQuery();
- CON.Close();
- }
- public void readTable(string tablename, List<int> rows)
- {
- DataSet DS = new DataSet();
- DataTable DT = new DataTable();
- CON.Open();
- NpgsqlDataAdapter DA = new NpgsqlDataAdapter(("SELECT * FROM " + tablename), CON);
- DA.Fill(DS);
- DT = DS.Tables[0];
- foreach (DataRow ROW in DT.Rows)
- {
- for (int i = 0; i < rows.Count; i++)
- {
- Console.Write(ROW[rows[i]].ToString() + " ");
- }
- Console.Write("\n");
- }
- CON.Close();
- }
- public int straatnaamtoint(string straatnaam, string postcode)
- {
- NpgsqlCommand COM = new NpgsqlCommand();
- COM.Connection = CON;
- GeneralParser GP = new GeneralParser();
- postcode = postcode.Replace(" ", "");
- Console.WriteLine("Trying to convert " + postcode + " to wijk");
- string wijk = PostcodeNaarWijk(postcode);
- if (wijk != "COULDNOTCONVERT")
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Managed to convert " + postcode + " to wijk " + wijk);
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Did not manage to convert " + postcode);
- }
- Console.ForegroundColor = ConsoleColor.White;
- if (allestraten.Count != 0)
- {
- for (int i = 0; i < allestraten.Count - 1; i++)
- {
- if (allestraten[i].straatnaam == straatnaam)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("MATCH!");
- Console.WriteLine("FOUND " + straatnaam + " ON ID " + allestraten[i].id.ToString());
- System.Threading.Thread.Sleep(10);
- return allestraten[i].id;
- }
- }
- }
- System.Threading.Thread.Sleep(30);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("NO MATCH!");
- allestraten.Add(new straatnamen(straatnaam, wijk, allestraten.Count));
- Console.WriteLine("ADDING " + straatnaam + "TO DATABASE ON ID " + allestraten[allestraten.Count - 1].id.ToString());
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- CON.Open();
- COM.Connection = CON;
- COM.CommandText = "INSERT INTO straatnaam(id, straatnaam, wijk) VALUES ( " + allestraten[allestraten.Count - 1].id.ToString() + ", '" + allestraten[allestraten.Count - 1].straatnaam.Replace("'", "") + "', '" + allestraten[allestraten.Count - 1].wijk + "');\n";
- try
- {
- COM.ExecuteNonQuery();
- Console.WriteLine(COM.CommandText);
- }
- catch (Exception e)
- {
- }
- CON.Close();
- return allestraten[allestraten.Count - 1].id;
- }
- public int straatnaamtoint2(string straatnaam)
- {
- NpgsqlCommand COM = new NpgsqlCommand();
- COM.Connection = CON;
- GeneralParser GP = new GeneralParser();
- for (int i = 0; i < allestraten.Count; i++)
- {
- if (allestraten[i].straatnaam.ToUpper().Contains("ZUIDWIJ"))
- {
- Console.WriteLine(allestraten[i].straatnaam);
- }
- if (allestraten[i].straatnaam.ToUpper().Replace(" ", "") == straatnaam.ToUpper().Replace(" ", ""))
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("MATCH!");
- Console.WriteLine("FOUND " + straatnaam + " ON ID " + allestraten[i].id.ToString());
- System.Threading.Thread.Sleep(10);
- return allestraten[i].id;
- }
- }
- return 0;
- }
- public string PostcodeNaarWijk(string postcode)
- {
- for (int i = 0; i < Postcodesenwijken.Count; i++)
- {
- if (Postcodesenwijken[i][0].Contains(postcode))
- {
- return Postcodesenwijken[i][1].ToString();
- }
- }
- return "COULDNOTFINDWIJK";
- }
- public string findWijk(int straatint)
- {
- for (int i = 0; i < allestraten.Count; i++)
- {
- if (allestraten[i].id == straatint)
- {
- return allestraten[i].wijk;
- }
- }
- return "";
- }
- }
- public class straatnamen
- {
- public string straatnaam;
- public string wijk;
- public int id;
- public straatnamen(string straatnaam, string wijk, int id)
- {
- this.straatnaam = straatnaam;
- this.wijk = wijk;
- this.id = id;
- }
- }
- }
Add Comment
Please, Sign In to add comment