Csharp/CSharp Tutorial/ADO.Net/Data Provider

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create Database connection from DbProviderFactory

using System;
using System.Data;
using System.Data.rumon;
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"]);
                }
            }
            
        }
    }

Get the list of ADO.NET data providers registered in the machine and application configuration file

using System;
using System.Data;
using System.Data.rumon;
class MainClass
{
    public static void Main(string[] args)
    {
        using (DataTable providers = DbProviderFactories.GetFactoryClasses())
        {
            Console.WriteLine("Available ADO.NET Data Providers:");
            foreach (DataRow prov in providers.Rows)
            {
                Console.WriteLine("Name:{0}", prov["Name"]);
                Console.WriteLine("Description:{0}", prov["Description"]);
                Console.WriteLine("Invariant Name:{0}", prov["InvariantName"]);
            }
        }
    }
}
Available ADO.NET Data Providers:
Name:Odbc Data Provider
Description:.Net Framework Data Provider for Odbc
Invariant Name:System.Data.Odbc
Name:OleDb Data Provider
Description:.Net Framework Data Provider for OleDb
Invariant Name:System.Data.OleDb
Name:OracleClient Data Provider
Description:.Net Framework Data Provider for Oracle
Invariant Name:System.Data.OracleClient
Name:SqlClient Data Provider
Description:.Net Framework Data Provider for SqlServer
Invariant Name:System.Data.SqlClient

OleDbProvider

using System;
using System.Data;
using System.Data.OleDb;
    class OleDbProvider
    {
        static void Main(string[] args)
        {
            string connString = @"provider = sqloledb;data source = .\sqlexpress;integrated security = sspi;initial catalog = northwind";
            string sql = @"select * from employees";
            OleDbConnection conn = null;
            OleDbDataReader reader = null;
            try
            {
                conn = new OleDbConnection(connString);
                conn.Open();
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(
                       "{0} | {1}"
                     , reader["FirstName"].ToString().PadLeft(10)
                     , reader[1].ToString().PadLeft(10)
                    );
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
            finally
            {
                reader.Close();
                conn.Close();
            } 
        }
    }