Csharp/C Sharp by API/System.Data.SqlClient/SqlConnection

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

new SqlConnection(String connection)

  
using System;
using System.Data.SqlClient;
class MainClass 
{
  static void Main(string[] args)
  {
    SqlConnection MyConnection = new SqlConnection("Data Source=(local); Initial  Catalog = MyDatabase; User ID=sa;Password=");
    MyConnection.Open();
  }
}


SqlConnection.BeginTransaction()

 
using System;
using System.Data;
using System.Data.SqlClient;
class ExecuteTransaction
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
    mySqlConnection.Open();
    SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.Transaction = mySqlTransaction;
    mySqlCommand.rumandText =
      "INSERT INTO Employee (" +
      "  ID, FirstName" +
      ") VALUES (" +
      "  10, "Jason"" +
      ")";
    Console.WriteLine("Running first INSERT statement");
    mySqlCommand.ExecuteNonQuery();
    mySqlCommand.rumandText =
      "INSERT INTO Employee (" +
      "  ID, FirstName" +
      ") VALUES (" +
      "  11, "Jason"" +
      ")";
    Console.WriteLine("Running second INSERT statement");
    mySqlCommand.ExecuteNonQuery();
    Console.WriteLine("Committing transaction");
    mySqlTransaction.rumit();
    mySqlConnection.Close();
  }
}


SqlConnection.ChangeDatabase

 

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class MainClass{
   public static void Main(){
    SqlConnection cn = new SqlConnection();
    try
    {
      cn.StateChange += new StateChangeEventHandler(StateChange);
      cn.InfoMessage += new SqlInfoMessageEventHandler(InfoMessage);
      cn.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
      cn.Open();
      cn.ChangeDatabase("Biblio");
    }
    catch( Exception ex )
    {
      Console.WriteLine(ex.Message);
    }
    finally
    {
      // Unsubscribe from the Events we subscribed to.
      cn.StateChange -= new StateChangeEventHandler(StateChange);
      cn.InfoMessage -= new SqlInfoMessageEventHandler(InfoMessage);
    }
   }
  private static void StateChange(object sender, System.Data.StateChangeEventArgs e)
  {
    Console.WriteLine("State was:" + e.OriginalState.ToString() + ". New state = " + e.CurrentState.ToString() );
  }
  
  private static void InfoMessage( object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
  {
    int i;
    for (i=0; i < e.Errors.Count; i++ )
    {
      Console.WriteLine(e.Errors[i].Message);
    }
  }
}


SqlConnection.ConnectionString

 
using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.ConnectionTimeout

 

using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.Database

 
using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.DataSource

 
using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.InfoMessage

 

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class MainClass{
   public static void Main(){
    SqlConnection cn = new SqlConnection();
    try
    {
      cn.StateChange += new StateChangeEventHandler(StateChange);
      cn.InfoMessage += new SqlInfoMessageEventHandler(InfoMessage);
      cn.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
      cn.Open();
      cn.ChangeDatabase("Biblio");
    }
    catch( Exception ex )
    {
      Console.WriteLine(ex.Message);
    }
    finally
    {
      // Unsubscribe from the Events we subscribed to.
      cn.StateChange -= new StateChangeEventHandler(StateChange);
      cn.InfoMessage -= new SqlInfoMessageEventHandler(InfoMessage);
    }
   }
  private static void StateChange(object sender, System.Data.StateChangeEventArgs e)
  {
    Console.WriteLine("State was:" + e.OriginalState.ToString() + ". New state = " + e.CurrentState.ToString() );
  }
  
  private static void InfoMessage( object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
  {
    int i;
    for (i=0; i < e.Errors.Count; i++ )
    {
      Console.WriteLine(e.Errors[i].Message);
    }
  }
}


SqlConnection.PacketSize

 
using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.ServerVersion

 
using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.State

 
using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}


SqlConnection.StateChange

 

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class MainClass{
   public static void Main(){
    SqlConnection cn = new SqlConnection();
    try
    {
      cn.StateChange += new StateChangeEventHandler(StateChange);
      cn.InfoMessage += new SqlInfoMessageEventHandler(InfoMessage);
      cn.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
      cn.Open();
      cn.ChangeDatabase("Biblio");
    }
    catch( Exception ex )
    {
      Console.WriteLine(ex.Message);
    }
    finally
    {
      // Unsubscribe from the Events we subscribed to.
      cn.StateChange -= new StateChangeEventHandler(StateChange);
      cn.InfoMessage -= new SqlInfoMessageEventHandler(InfoMessage);
    }
   }
  private static void StateChange(object sender, System.Data.StateChangeEventArgs e)
  {
    Console.WriteLine("State was:" + e.OriginalState.ToString() + ". New state = " + e.CurrentState.ToString() );
  }
  
  private static void InfoMessage( object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
  {
    int i;
    for (i=0; i < e.Errors.Count; i++ )
    {
      Console.WriteLine(e.Errors[i].Message);
    }
  }
}


SqlConnection.WorkstationId

 

using System;
using System.Data;
using System.Data.SqlClient;
class MySqlConnection {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    SqlConnection mySqlConnection = new SqlConnection(connectionString);
    mySqlConnection.Open();
    Console.WriteLine("mySqlConnection.ConnectionString = "+mySqlConnection.ConnectionString);
    Console.WriteLine("mySqlConnection.ConnectionTimeout = "+mySqlConnection.ConnectionTimeout);
    Console.WriteLine("mySqlConnection.Database = "+mySqlConnection.Database);
    Console.WriteLine("mySqlConnection.DataSource = "+mySqlConnection.DataSource);
    Console.WriteLine("mySqlConnection.PacketSize = "+mySqlConnection.PacketSize);
    Console.WriteLine("mySqlConnection.ServerVersion = "+mySqlConnection.ServerVersion);
    Console.WriteLine("mySqlConnection.State = "+mySqlConnection.State);
    Console.WriteLine("mySqlConnection.WorkstationId = "+mySqlConnection.WorkstationId);
    mySqlConnection.Close();
  }
}