// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; using System.Data.Odbc; public class MultiDb { public static IDbConnection GetConnection(string constr) { if(constr.ToUpper().IndexOf("DRIVER=") >= 0) { return new OdbcConnection(constr); } else if(constr.ToUpper().IndexOf("PROVIDER=") >= 0) { return new OleDbConnection(constr); } else if(constr.ToUpper().IndexOf("TRUSTED_CONNECTION=") >= 0 || constr.ToUpper().IndexOf("INTEGRATED SECURITY=") >= 0) { return new SqlConnection(constr); } else { return null; } } } class MultiDb11 { private static void test(string constr) { IDbConnection con = MultiDb.GetConnection(constr); con.Open(); IDbCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT * FROM T1"; IDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { int f1 = (int)rdr[0]; string f2 = (string)rdr[1]; Console.WriteLine(f1 + " " + f2); } con.Close(); } public static void Main(string[] args) { test(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Databases\MSAccess\Test.mdb"); test("server=ARNEPC3;Integrated Security=SSPI;database=Test"); } }