using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using System.Windows.Forms;
using ABC.File;

namespace ABC.KetNoi
{
    class T_Connect_master
    {
        public DataSet T_opendataset(string strsql)
        {
            string chuoi = Get_CauHinh.Dt_DAT.Rows[0]["Connect_String"].ToString();
            chuoi = chuoi.Replace("initial catalog=ABC", "initial catalog=master");
            SqlConnection conn = new SqlConnection(chuoi);
            SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                da.Fill(ds);
                conn.Close();
                return ds;
            }
            catch
            {
                return null;
            }
        }
        Error_Form _Error_Form;
        string sql_sender;
        public DataSet T_open(string sql)
        {
            sql_sender = sql;
            DataSet dts = T_opendataset(sql);
            if (dts != null)
            {
                return dts;
            }
            else
            {
                _Error_Form = new Error_Form();
                _Error_Form.error_text = sql;
                _Error_Form.FormClosed += new System.Windows.Forms.FormClosedEventHandler(_Error_Form_FormClosed);
                _Error_Form.ShowDialog();
                if (_Error_Form.DialogResult == DialogResult.OK)
                {
                    return T_open(sql);
                }
                else
                {
                    
                    return null;
                   
                }
            }
        }

        void _Error_Form_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            if (_Error_Form.DialogResult == DialogResult.Cancel)
            {
                System.Environment.Exit(1);
            }
            else
            {
                T_open(sql_sender);
            }
        }

        protected bool T_executeupdatesqlstr(string strsql)
        {
            string chuoi = Get_CauHinh.Dt_DAT.Rows[0]["Connect_String"].ToString();
            chuoi = chuoi.Replace("initial catalog=ABC", "initial catalog=master");
            SqlConnection conn = new SqlConnection(chuoi);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = strsql;
            cmd.CommandType = CommandType.Text;
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void T_exc(string sql)
        {
            sql_sender = sql;
            if (T_executeupdatesqlstr(sql) == true)
            {
                
            }
            else
            {
                _Error_Form = new Error_Form();
                _Error_Form.error_text = sql;
                _Error_Form.FormClosed +=new FormClosedEventHandler(_Error_Form_FormClosed);
                _Error_Form.ShowDialog();
              

            }
        }
    }
}
