Create Database connection from DbProviderFactory : Data Provider « ADO.Net « C# / CSharp Tutorial






using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = "data source=localhost; initial catalog=SampleDB; Integrated Security=SSPI;";
            conn.Open();

            if (factory.CanCreateDataSourceEnumerator)
            {
                DbDataSourceEnumerator dsEnum = factory.CreateDataSourceEnumerator();
                DataTable sources = dsEnum.GetDataSources();
                foreach (DataRow dataSource in sources.Rows)
                {
                    Console.WriteLine(dataSource["ServerName"]);
                }
            }
            
        }
    }








32.31.Data Provider
32.31.1.Get the list of ADO.NET data providers registered in the machine and application configuration file
32.31.2.Create Database connection from DbProviderFactory
32.31.3.OleDbProvider