using System;
using System.Windows.Forms;
//-------------------------------------
using System.Data;
using System.Data.SqlClient;

namespace _ex_17_42
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sCon = new SqlConnection(@"Server=.;
                                                    DataBase=MyTestDB;
                                                    Integrated Security=True");
            
            sCon.Open();
            
            if (sCon.State == ConnectionState.Open)
                MessageBox.Show("اتصال به بانک اطلاعاتی برقرار است");
            else
                MessageBox.Show("اتصال به بانک اطلاعاتی برقرار نیست");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection sCon = new SqlConnection(@"Server=.;
                                                    DataBase=MyTestDB;
                                                    Integrated Security=True");
            
            try
            {
                if (sCon.State != ConnectionState.Open)
                    sCon.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sCon.Close();
            }
        }
    }
}