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

using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace ABC.KetNoi
{
    class Class_KetNoi_ToaNhan
    {
        string str = "";
        String Server = "", User = "", Pass = "", Data = "";
        SqlConnection conn = new SqlConnection();
        public Class_KetNoi_ToaNhan()
        {
            string path = Application.StartupPath + "/Data.dat";
            if (System.IO.File.Exists(path))
            {
                using (BinaryReader reader = new BinaryReader(System.IO.File.Open(path, FileMode.Open)))
                {

                    string a = reader.ReadString();
                    // HinhThuc = reader.ReadInt32();
                    string b = reader.ReadString();
                    string c = reader.ReadString();
                    string d = reader.ReadString();
                    string e1 = reader.ReadString();
                    string e2 = reader.ReadString();
                    string e3 = reader.ReadString();
                    string e4 = reader.ReadString();
                    string e5 = reader.ReadString();
                    string e6 = reader.ReadString();
                    string e7 = reader.ReadString();
                    string e8 = reader.ReadString();
                    string e9 = reader.ReadString();
                    string e10 = reader.ReadString();
                    string e11 = reader.ReadString();
                    //SerVer
                    string[] ar = a.Split(',');
                    byte[] ser = new byte[ar.Length];
                    for (int i = 0; i < ar.Length; i++)
                    {
                        ser[i] = Byte.Parse(ar[i]);
                    }
                    Server = Encoding.ASCII.GetString(ser);
                    //User
                    string[] u = b.Split(',');
                    byte[] use = new byte[u.Length];
                    for (int j = 0; j < u.Length; j++)
                    {
                        use[j] = Byte.Parse(u[j]);
                    }
                    User = Encoding.ASCII.GetString(use);

                    //Pass
                    string[] pa = c.Split(',');
                    byte[] pas = new byte[pa.Length];
                    for (int k = 0; k < pa.Length; k++)
                    {
                        pas[k] = Byte.Parse(pa[k]);
                    }
                    Pass = Encoding.ASCII.GetString(pas);
                    //Data
                    string[] da = d.Split(',');
                    byte[] dat = new byte[da.Length];
                    for (int l = 0; l < da.Length; l++)
                    {
                        dat[l] = Byte.Parse(da[l]);
                    }
                    Data = Encoding.ASCII.GetString(dat);
                    //
                    //data nhận/gởi toa
                    //string[] goi_toa = e11.Split(',');
                    //byte[] dagoi_toa = new byte[goi_toa.Length];
                    //for (int l = 0; l < goi_toa.Length; l++)
                    //{
                    //    dagoi_toa[l] = Byte.Parse(goi_toa[l]);
                    //}
                    //Data = Encoding.UTF8.GetString(dagoi_toa);

                }
                str = string.Format("server={0};database={1};uid={2};pwd={3}", Server, Data, User, Pass);

            }
            conn.ConnectionString = str;
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            
        }
        public void SaoLuu(string tGian)
        {

            string key = ConfigurationManager.AppSettings["backup"].ToString();
            T_exc(string.Format("BACKUP DATABASE {0} TO  DISK = '{1}'", Data, Data + "_" + tGian + ".bak"));


        }
        public void XoaDLKetChuyen(int soNgay)
        {
           
            SqlCommand cmd = new SqlCommand("sp_KetChuyen", conn);
            cmd.Parameters.AddWithValue("@Ngay", soNgay);
            cmd.CommandType = CommandType.StoredProcedure;
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
        public DataSet T_opendataset(string strsql)
        {

            string chuoi = str;           
            SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
            DataSet ds = new DataSet();
            try
            {
                if(conn.State==ConnectionState.Closed)
                   conn.Open();
                da.Fill(ds);
                conn.Close();
                return ds;
            }
            catch
            {
                return null;
            }
        }
        public DataSet T_opendataset_store(string strsql,string[]name, object[]value, int para)
        {
            string chuoi = str;          
            SqlCommand command = new SqlCommand(strsql, conn);
            for (int i = 0; i < para; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                da.Fill(ds);
                conn.Close();
                return ds;
            }
            catch
            {
                return null;
            }
        }
        public int updateTable(string strsql)
        {
            string chuoi = str;
            SqlCommand command = new SqlCommand(strsql, conn);
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                return command.ExecuteNonQuery();
            }
            catch (Exception)
            {

                return 0;
            }
          
        }
        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);
            }
        }

        public bool T_executeupdatesqlstr(string strsql)
        {
            string chuoi = str;          
            SqlCommand cmd = new SqlCommand();          
            cmd.CommandText = strsql;
            cmd.CommandType = CommandType.Text;
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                cmd.Connection = conn;
                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();
              

            }
        }
    }
}
