using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

//System.Configuration.dll Verweis hinzufügen
using System.Configuration;

namespace ConnectionApp
{
   public partial class Form1 : Form
   {
      SqlConnection connection;
      ConnectionStringSettings connectionInfo;

      public Form1()
      {
         InitializeComponent();

         connectionInfo = ConfigurationManager.ConnectionStrings["ConnectionOne"];

         connection = new SqlConnection(connectionInfo.ConnectionString);
      }

      private void button1_Click(object sender, EventArgs e)
      {
         connection.InfoMessage += delegate(object s, SqlInfoMessageEventArgs a)
         {
            textBox1.Text += "Message:" + a.Message + "\r\n";
            textBox1.Text += "Source:" + a.Source + "\r\n";
         };
         connection.StateChange += delegate(object s, StateChangeEventArgs a)
         {
            textBox1.Text += "OriginalState:" + a.OriginalState.ToString() + "\r\n";
            textBox1.Text += "CurrentState:" + a.CurrentState.ToString() + "\r\n";
         };

         connection.Open();
      }

      //void connection_StateChange(object sender, StateChangeEventArgs e)
      //{
      //   textBox1.Text += "OriginalState:" + e.OriginalState.ToString() + "\r\n";
      //   textBox1.Text += "CurrentState:" + e.CurrentState.ToString() + "\r\n";
      //}

      //void connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
      //{
      //   textBox1.Text += "Message:" + e.Message + "\r\n";
      //   textBox1.Text += "Source:" + e.Source + "\r\n";
      //}

      private void button2_Click(object sender, EventArgs e)
      {
         connection.Close();
      }

      private void button3_Click(object sender, EventArgs e)
      {
         SqlConnection connection;

         using (connection = new SqlConnection(connectionInfo.ConnectionString))
         {
            textBox1.Text += connection.State.ToString() + "\r\n";
            connection.Open();
            textBox1.Text += connection.State.ToString() + "\r\n";
         }
         textBox1.Text += connection.State.ToString() + "\r\n";

         SqlConnection.ClearPool(connection);
      }
   }
}