OleDbConnection cn = null; try { // Verbindung zu DB herstellen string strProvider = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=..\Test.mdb); cn = new OleDbConnection(strProvider); cn.Open(); // Datensätze durchlaufen OleDbCommand cmd = cn.CreateCommand(); cmd.CommandText = "SELECT * FROM Kunden;"; OleDbDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { Console.WriteLine( "{0}, {1}", dr["Vorname"].ToString() , dr["Name"].ToString() , ); } } catch(Exception ex) { Console.WriteLine(ex.Message); } finally { con.Close(); // Verbindung wieder schliessen }
IDbConnection con; // Variable vom gemeinsamen Typ der generischen Schnittstelle con = new OleDbConnection(); //... con = new SqlConnection();
using Jakko.Databases; // Namespace referenzieren, der diese Spezialklasse enthält //... IDbConnection cn = null; try { // Fabrik-Objekt erzeugen ProviderFactory factory = new ProviderFactory(); // Connection-Objekt von Fabrik anfordern und Verbindung herstellen cn = factory.GetConnection(); cn.Open(); // Datensätze durchlaufen IDbCommand cmd = cn.CreateCommand(); cmd.CommandText = "SELECT * FROM Kunden;"; IDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { Console.WriteLine( "{0}, {1}", dr["Vorname"].ToString() , dr["Name"].ToString() , ); } } catch(Exception ex) { Console.WriteLine(ex.Message); // Oops, da is' was schief gelaufen } finally { con.Close(); // Verbindung wieder schliessen }
using System; using System.Configuration; using System.Data; using System.Data.OleDb; using System.Data.Odbc; using ByteFX.Data.MySqlClient; using System.Data.SqlClient; namespace Jakko.Databases { // Aufzählungstyp für die verschiedenen // Datenbanktypen public enum ProviderType { MySQL = 1, OleDb = 2, MSSQL = 3, ODBC = 4 }; // Fabrik-Klasse für ADO.NET-Objekte class ProviderFactory { public class ProviderFactory { // // Private Daten // private ProviderType providerType; private string strConnection; // // Konstruktoren // public ProviderFactory() { // Informationen für Datenbankzugriff aus Konfigurations- // datei lesen string strProviderType = ConfigurationSettings.AppSettings["ProviderType"]; this.providerType = (ProviderType) Enum.Parse( typeof(ProviderType), strProviderType, true) ; this.strConnection = ConfigurationSettings.AppSettings["Connection"]; } public ProviderFactory (ProviderType type, string strConnection) { // Private Datenfelder mit Werten aus // Argumenten besetzen this.providerType = type; this.strConnection = strConnection; } // // Methoden // public IDbConnection GetConnection() { IDbConnection con; switch(providerType) { case ProviderType.OleDb: con = new OleDbConnection(this.strConnection); break; case ProviderType.ODBC: con = new OdbcConnection(this.strConnection); break; case ProviderType.MySQL: con = new MySqlConnection(this.strConnection); break; case ProviderType.MSSQL: con = new SqlConnection(this.strConnection); break; default: throw new Exception ("Unbekannter Provider"); } return con; }// GetConnection() // weitere Methoden nach dem selben Scheme // GetDataAdapter() etc. }//class }// namespace