Autor Beitrag
Raven280438
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Di 06.12.11 14:00 
Hi,

ich habe 2 DLLs die ich als Verweis in das Projekt eingefügt habe.
Beide DLLs haben die gleichen Namespaces (in unterschiedlichen Versionen).

Kann ich irgendwie auf einen Namespace einer bestimmten DLL zugreifen?

Hintergrund:
Ich arbeite mit dem MySQL-Connector. Die neuste Version unterschützt aber nichtmehr Mysql 4.0. Ich wollte versuchen, den Connector in verschiedenen Versionen zu benutzen, je nach Mysql-Version des Servers.

Hat jemand ne Idee?

Gruß
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 06.12.11 14:30 
Erst rausfinden welche der beiden Assemblies du brauchst und dann die passende dynamisch laden?
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Di 06.12.11 15:46 
Hi,

danke für die Info.

Ich hab beim Recherchieren diese Seite gefunden.

Dabei wird eine Klasse aus einer DLL geladen. Allerdings hat die Klasse die ich benötige im Konstruktor noch eine Variable. Wie kann ich den Code anpassen, dass ich dem Konstruktor noch eine Variable übergeben kann?
Wie kann ich danach in der Methode "Open" die Open-Methode von der MySQLConnection-Klasse aufrufen (woher weis die Klasse, dass sie eine Methode hat)?


Hier mein Aktueller Code:
ausblenden volle Höhe C#-Quelltext
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:
    class MySQL50
    {
        private String DLLName = "MySql.Data.dll";
        private Assembly A = null;

        private object MySQLConnection = null;

        public MySQL50(String ConnString)
        {
            this.A = Assembly.LoadFile(this.DLLName);

            Type t = A.GetType("MySql.Data.MySqlClient.MySqlConnection");

            this.MySQLConnection = Activator.CreateInstance(t);
        }

        public bool Open()
        {
            try
            {
                this.MySQLConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }

            return true;
        }
    }



Gruß
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 06.12.11 15:53 
Activator.CreateInstance hat auch eine Überladung der man Argumente für den Constructor mitgeben kann.
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Di 06.12.11 15:54 
Hi,

Ok danke,

Wie verhält es sich mit de Open-Methode? Muss ich dafür ein Interface machen?


Gruß
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 06.12.11 15:57 
Zitat:
private object MySQLConnection = null;


aus object solltest du DbConnection machen. Vereinfacht die Handhabung. Gilt genauso für die anderen Object wie DbCommand, DbDataReader, DbTransaction und was man sonst so braucht. Wenn deine DatenbankWrapper Klasse diese Typen veröffentlicht solltest du die genauso verwenden können als würdest du eine konkrete Implementierung verwenden.
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Di 06.12.11 16:00 
Hi,

ich hoffe ich hab das richtig verstanden ;)

Woher kenne ich aber die Klasse "MySqlConnection"? Muss ich dazu eine der DLLs dem Projekt als Verweis hinzufügen? Oder wo bekomm ich die Klasse her?



Gruß
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 06.12.11 16:04 
MySqlConnection musst du nicht kennen (sind ja auch in deinem Fall 2 verschiedene Klassen) sondern nur DBConnection. Gegen DBConnection(die Basisklasse für alle Connection Klassen) solltest du genauso arbeiten können wie gegen MySqlConnection.
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Mi 07.12.11 11:01 
Hi,

danke für die Hilfe, ich habs hinbekommen.
Hier die komplette Klasse, falls irgendjemand das gleiche Problem hat:

ausblenden volle Höhe C#-Quelltext
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:
class MySQL5
    {
        private String DLLName = "MySql.Data.dll";
        private Assembly A = null;

        private DbConnection MySQLConnection = null;
        private DbDataAdapter DataAdapter = null;
        private DbCommand Command = null;

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="ConnString">Verbindungs-String</param>
        public MySQL5(String ConnString)
        {
            this.A = Assembly.LoadFile(Path.Combine(Application.StartupPath,this.DLLName));

            Type t = A.GetType("MySql.Data.MySqlClient.MySqlConnection");
            Type ta = A.GetType("MySql.Data.MySqlClient.MySqlDataAdapter");
            Type tc = A.GetType("MySql.Data.MySqlClient.MySqlCommand");

            this.MySQLConnection = (DbConnection)Activator.CreateInstance(t,new object[] { ConnString });

            this.DataAdapter = (DbDataAdapter)Activator.CreateInstance(ta, null);

            this.Command = (DbCommand)Activator.CreateInstance(tc, null);
        }

        /// <summary>
        /// Verbindung öffnen
        /// </summary>
        /// <returns>True bei Erfolg, False im Fehlerfall</returns>
        public bool Open()
        {
            try
            {
                this.MySQLConnection.Open();
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Sendet einen Query und gibt die Rückgabe zurück
        /// </summary>
        /// <param name="query">MySQL-Query</param>
        /// <returns>Rückgabe als DataSet</returns>
        public DataSet SendQuery(String query)
        {
            this.Command.CommandText = query;
            this.Command.Connection = this.MySQLConnection;


            this.DataAdapter.SelectCommand = this.Command;

            DataSet ds = new DataSet();

            this.DataAdapter.Fill(ds);

            return ds;
        }

        /// <summary>
        /// Schließt die Verbindung
        /// </summary>
        public void Close()
        {
            this.MySQLConnection.Close();
        }
    }



Gruß
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 07.12.11 11:19 
Dataset als Rückgabetyp von SendQuery ist etwas übertrieben. Die würde eh nur eine DataTable enthalten da reicht es dann auch gleich bei DataTable zu bleiben.

Für diesen Beitrag haben gedankt: Raven280438