-
Notifications
You must be signed in to change notification settings - Fork 0
/
URL.cs
76 lines (66 loc) · 2.67 KB
/
URL.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
using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
namespace DatasetProg
{
internal class Url
{
// creates connection variable using Linux server login credentials and ADO.NET framework
private readonly MySqlConnection _conn =
new MySqlConnection("Server = danu6.it.nuigalway.ie; Database = mydb2463; Uid = mydb2463ca; Pwd = mi3tax");
// create private read only list of type StationData to store data from DB
private readonly List<StationData> _stationInfo = new List<StationData>();
// creates reader
private MySqlDataReader _reader;
public List<StationData> ReadTable()
{
try
{
// open connection
_conn.Open();
// read using an SQL select stateent from table with constant string variable
const string readDb = @"SELECT * FROM Station;";
var cmdRead = new MySqlCommand(readDb, _conn);
// execute command & assign to new variable
_reader = cmdRead.ExecuteReader();
// while reader is open
while (_reader.Read())
{
// create new stationData object using the object initializer
var stationData = new StationData
{
Longitude = (string) _reader["longitude"],
Latitude = (string) _reader["latitude"],
StationId = (string) _reader["stationId"],
Csv = (string) _reader["csv"],
Json = (string) _reader["json"]
};
// write out columns to confirm data
Console.WriteLine(_reader["longitude"] + " " + _reader["latitude"] + " " + _reader["stationID"]);
// adds stationData to list
_stationInfo.Add(stationData);
}
}
catch (Exception ex)
{
// outputs error message
Console.WriteLine(ex.Message);
}
finally
{
// close the reader with null propogation
_reader?.Close();
// close the connection with null propogation
_conn?.Close();
// used as an escape method when testing method function
//Console.ReadKey();
}
return _stationInfo;
}
// method to return _stationInfo List collection for testing
public List<StationData> GetList()
{
return _stationInfo;
}
}
}