DbProviderFactory.CreateConnection : DbProviderFactory « System.Data.Common « C# / C Sharp by API






DbProviderFactory.CreateConnection

  

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"]);
                    }
                }
            }
        }

    }
}

   
    
  








Related examples in the same category