Blog Content

    티스토리 뷰

    23.ADO.NET - ConnectionEvent

    반응형
    윈 폼에서 ADO를 연결 상태를 확인 하는 예제입니다.


    Form1.Designer.cs

    [그림23-1]


         private System.Windows.Forms.ListBox listBox1;

         private System.Windows.Forms.Button button1;

         private System.Windows.Forms.Button button2;


    Form1.cs

    public partial class Form1 : Form

    {

       private SqlConnection Con;

     

       public Form1()

       {

           InitializeComponent();

       }

     

       private void Form1_Load(object sender, EventArgs e)

       {

           Con = new SqlConnection();

           Con.ConnectionString =

          "Server=WINDOWS-XP\\SQLSERVER;database=Test;uid=Test;pwd=1234;Integrated

          Security=true;";

           Con.StateChange += new StateChangeEventHandler(Con_StateChange);

           Con.InfoMessage += new SqlInfoMessageEventHandler(Con_InfoMessage);

          

       }

     

       void Con_InfoMessage(object sender, SqlInfoMessageEventArgs e)

       {

           listBox1.Items.Add(e.Message);

       }

     

       void Con_StateChange(object sender, StateChangeEventArgs e)

       {

           string Mes;

           Mes = string.Format("원래 상태 : {0}, 현재 상태 {1}", e.OriginalState, e.CurrentState);

           listBox1.Items.Add(Mes);

       }

     

       private void button1_Click(object sender, EventArgs e)

       {

           try

           {

               Con.Open();

           }

           catch (Exception ex)

           {

               MessageBox.Show(ex.Message);

           }

       }

     

       private void button2_Click(object sender, EventArgs e)

       {

           if (Con.State == ConnectionState.Open)

           {

               Con.Close();

           }

       }

     

       private void Form1_FontChanged(object sender, EventArgs e)

       {

           if (Con.State == ConnectionState.Open)

           {

               Con.Close();

           }

       }

     

       private void Form1_FontChanged_1(object sender, EventArgs e)

       {

           if (Con.State == ConnectionState.Open)

           {

               Con.Close();

           }

       }

    }


    결과화면

    [그림23-2]



    반응형

    'ASP.NET' 카테고리의 다른 글

    25.ADO.NET - DataAdapter  (0) 2009.09.30
    24.ADO.NET - CommandTest  (0) 2009.09.30
    -- DataRelation (Xml 변환)  (0) 2009.09.30
    22.ADO.NET - MakeDataSet  (0) 2009.09.29
    21.ADO.NET - DataTableSelect  (0) 2009.09.29

    Comments