Obtain the DbProviderFactory for SQL Server and create connection from it : DbProviderFactories « ADO.Net « C# / CSharp Tutorial






using System;
using System.Data;
using System.Data.Common;

class MainClass
{
    public static void Main(string[] args)
    {
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

        using (IDbConnection con = factory.CreateConnection())
        {
            con.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";

            using (IDbCommand com = con.CreateCommand())
            {
                com.CommandType = CommandType.Text;
                com.CommandText = "SELECT ID, FirstName FROM Employee";

                con.Open();

                using (IDataReader reader = com.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Display the product details.
                        Console.WriteLine("  {0} = {1}",
                            reader["ID"],
                            reader["FirstName"]);
                    }
                }
            }
        }

    }
}








32.42.DbProviderFactories
32.42.1.Obtain the DbProviderFactory for SQL Server and create connection from it
32.42.2.Get the available data provider factories
32.42.3.Data Provider Factory