Base Class til Access database adgang
Hej,Jeg vil gerne lave en Base class der skaffer data ind fra flere access databaser. Ved at angive filepath henter Classen så data ind i en defineret liste.
Det virker men...
1) er det måden I eksperter vil bruge? Vil I bygge Classen på en anden måde?
2) Jeg har defineret Classen "KKSDataPoint" - burde det egentlig ikke være en Struct ?
Koden er her:
public class KKSDataPoint
{
public DateTime dateAndTime { get; set; }
public double value { get; set; }
public string Unit { get; set; }
public string Description { get; set; }
}
public class GetAccessData
{
private const string connectionsStringPartOne = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
private const string connectionsStringPartTwo = ";User Id=admin;Password=;";
private string connectionString = null;
private OleDbConnection connection = null;
protected GetAccessData() // Base Class
{
}
protected void MakeConnectionsString(string filePath, string fileName)
{
try
{
this.connectionString = connectionsStringPartOne + @filePath + @fileName + connectionsStringPartTwo;
this.connection = new OleDbConnection(this.connectionString);
this.connection.Open();
}
catch (Exception ex)
{
MessageBox.Show("An error type of " + ex.GetType() + " has occured with error message: " + ex.Message);
}
}
protected List<KKSDataPoint> QueryData(string kKsKey, DateTime fromDateTime, DateTime toDateTime)
{
List<KKSDataPoint> lst = new List<KKSDataPoint>();
using (this.connection)
{
string queryString = "SELECT * FROM DATA WHERE "
+ " DATO >= @Value1 and DATO <= @Value2 AND [KKS KEY]='" + kKsKey + "' ORDER BY DATO";
using (OleDbCommand command = new OleDbCommand(queryString, connection))
{
command.Parameters.AddWithValue("Value1", fromDateTime);
command.Parameters.AddWithValue("Value2", toDateTime);
try
{
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
KKSDataPoint data = new KKSDataPoint();
data.dateAndTime = (DateTime)reader["DATO"];
data.value = (double)reader["VAERDI"];
data.Description = description;
data.Unit = unit;
lst.Add(data);
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
return lst;
}
}