Csharp/CSharp Tutorial/ADO.Net/SqlConnectionStringBuilder

Материал из .Net Framework эксперт
Версия от 12:19, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Build a connection string from component parameters and display it

using System;
using System.Data.SqlClient;
class MainClass
{
    public static void Main(string[] args)
    {
        
        SqlConnectionStringBuilder sb2 = new SqlConnectionStringBuilder();
        sb2.DataSource = @".\sqlexpress";
        sb2.InitialCatalog = "Northwind";
        sb2.IntegratedSecurity = true;
        sb2.MinPoolSize = 5;
        sb2.MinPoolSize = 15;
        sb2.LoadBalanceTimeout = 600;
        sb2.ConnectionReset = true;
        Console.WriteLine("Constructed connection string:");
        Console.WriteLine("  " + sb2.ConnectionString);
    }
}
Constructed connection string:
  Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True;Min Pool Size=15;Conne
ction Reset=True;Load Balance Timeout=600

BuildConnectionString solution

using System;
using System.Data.SqlClient;
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(  );
            csb.DataSource = "(local)";
            csb.Add("Initial Catalog", "AdventureWorks");
            csb["Integrated Security"] = true;
            Console.WriteLine("Connection string:\n{0}", csb.ConnectionString);
            SqlConnection connection = new SqlConnection(csb.ConnectionString);
            connection.Open(  );
            Console.WriteLine("\nConnectionState = {0}", connection.State);
            connection.Close(  );
            Console.WriteLine("ConnectionState = {0}", connection.State);

        }
    }

Build SqlConnectionStringBuilder from connection string

using System;
using System.Data.SqlClient;
class MainClass
{
    public static void Main(string[] args)
    {
        string conString = @"Data Source=.\sqlexpress;" +
            "Database=Northwind;Integrated Security=SSPI;" +
            "Min Pool Size=5;Max Pool Size=15;Connection Reset=True;" +
            "Connection Lifetime=600;";
        SqlConnectionStringBuilder sb1 = new SqlConnectionStringBuilder(conString);
        Console.WriteLine("Parsed SQL Connection String Parameters:");
        Console.WriteLine("  Database Source = " + sb1.DataSource);
        Console.WriteLine("  Database = " + sb1.InitialCatalog);
        Console.WriteLine("  Use Integrated Security = " + sb1.IntegratedSecurity);
        Console.WriteLine("  Min Pool Size = " + sb1.MinPoolSize);
        Console.WriteLine("  Max Pool Size = " + sb1.MinPoolSize);
        Console.WriteLine("  Lifetime = " + sb1.LoadBalanceTimeout);
        Console.WriteLine("  Connection Reset = " + sb1.ConnectionReset);
   }
}
Parsed SQL Connection String Parameters:
  Database Source = .\sqlexpress
  Database = Northwind
  Use Integrated Security = True
  Min Pool Size = 5
  Max Pool Size = 5
  Lifetime = 600
  Connection Reset = True