// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 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 TestClass { 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=D:\\Database\\MSAccess\\Test.mdb"); test("server=ARNEPC2;Integrated Security=SSPI;database=Test"); } }