Avatar billede friiiiis Novice
11. januar 2015 - 21:49 Der er 7 kommentarer og
1 løsning

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;

        }

}
Avatar billede arne_v Ekspert
12. januar 2015 - 00:48 #1
re 1)

Jeg ville nok bruge en abstract readonly property til connection string.
Avatar billede arne_v Ekspert
12. januar 2015 - 00:48 #2
re 2)

Den kan baade vaere en struct eller en class. Vil du have value eller ref semantik?
Avatar billede arne_v Ekspert
12. januar 2015 - 04:26 #3
re 1)

Eller alternativt bare arg til constructor.
Avatar billede arne_v Ekspert
12. januar 2015 - 04:27 #4
Og jeg ville ikke holde connection aaben mellem kald.

.NET bruger en connection pool, saa der er meget lidt overhead ved at etablere en connection.
Avatar billede arne_v Ekspert
12. januar 2015 - 04:43 #5

using System;
using System.Data;
using System.Data.OleDb;

namespace E
{
    public abstract class DB
    {
        private string constr;
        protected abstract string DBFile { get; }
        public DB()
        {
            constr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};User Id=admin;Password=;", DBFile);
        }
        public void DoSomething()
        {
            using(OleDbConnection con = new OleDbConnection(constr))
            {
                con.Open();
                // ...
            }
        }
    }
    public class DBX : DB
    {
        protected override string DBFile
        {
            get { return  @"C:\Work\Database11.accdb"; }
        }
        public void DoMore()
        {
            DoSomething();
            // ...
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            DBX o = new DBX();
            o.DoMore();
            Console.ReadKey();
        }
    }
}


vs


using System;
using System.Data;
using System.Data.OleDb;

namespace E
{
    public class DB
    {
        private string constr;
        public DB(string dbfile)
        {
            constr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};User Id=admin;Password=;", dbfile);
        }
        public void DoSomething()
        {
            using(OleDbConnection con = new OleDbConnection(constr))
            {
                con.Open();
                // ...
            }
        }
    }
    public class DBX : DB
    {
        public DBX() : base(@"C:\Work\Database11.accdb")
        {
         
        }
        public void DoMore()
        {
            DoSomething();
            // ...
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            DBX o = new DBX();
            o.DoMore();
            Console.ReadKey();
        }
    }
}
Avatar billede friiiiis Novice
12. januar 2015 - 08:10 #6
Fantastisk svar Arne_V

A) Smid et svar!!

B) Hvad mener du med "Vil du have value eller ref semantik? "Du må gerne uddybe lidt...
Avatar billede arne_v Ekspert
12. januar 2015 - 15:38 #7
svar
Avatar billede arne_v Ekspert
12. januar 2015 - 15:39 #8
prov og koer dette lille program:


using System;

namespace E
{
    public class C
    {
        public int V;
    }
    public struct S
    {
        public int V;
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            C c1 = new C();
            c1.V = 123;
            C c2 = c1;
            c1.V = 456;
            Console.WriteLine(c1.V + " " + c2.V);
            S s1 = new S();
            s1.V = 123;
            S s2 = s1;
            s1.V = 456;
            Console.WriteLine(s1.V + " " + s2.V);
            Console.ReadKey();
        }
    }
}


Der er mange sammenhaenge hvor forskellen vil kunne maerkes men dette maa vaere den simpleste illustration.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester