using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using FMessegeBox;
using System.Data.SqlClient;
using FMessegeBox;
using System.Data;

namespace WindowsFormsApplication8
{
    class ConnectionInfo
    {
        string DataSource_ = Environment.MachineName;
        string InitialCatalog_;
        string UserId_;
        string Password_ = "";
        static string ConnectionString_;
        public static bool ConnectionStringAttachDb = true;
        //public static bool chDB;

        static SqlConnection con;
        static SqlDataAdapter ada = new SqlDataAdapter();
        static SqlCommand cmd = new SqlCommand();
        static DataSet ds = new DataSet();

        public void ConnectFile()
        {
            if (File.Exists(Application.StartupPath + @"\ServerInfo.ini"))
            {
                string Encrypt = File.ReadAllText(Application.StartupPath + @"\ServerInfo.ini");
                Security s = new Security();
                string ConInfo = s.Decrypt_Text(Encrypt, Security.Password_);
                string[] ConInfoA = ConInfo.Split('\n');

                DataSource_ = ConInfoA[0];
                InitialCatalog_ = ConInfoA[1];
                UserId_ = ConInfoA[2];
                Password_ = ConInfoA[3];
            }
        }

        public void ConnectDB_Info()
        {
            
            try
            {
                ConnectFile();
                //chDB_ = ClsSetting.CheckDB(DataSource, UserId, Password);
                if (ConnectionStringAttachDb)
                {
                    ConnectionString_ = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\SQL DataBase\\Tell.mdf;Integrated Security=True;User Instance=True";
                }
                else
                {
                    ConnectionString_ = "Data Source=" + DataSource_ + ";Initial Catalog=" + InitialCatalog_ + ";User Id=" + UserId_ + ";Password=" + Password_;
                }
                con = new SqlConnection(ConnectionString_);
                con.Open();

            }
            catch
            {
                FarsiMessegeBox.Show("بانک اطلاعاتی وجو ندارد و یا تنظیمات اتصال به بانک اطلاعاتی اشتباه است", "خطا", FMessegeBoxButtons.Ok, FMessegeBoxIcons.Error);
                frmPassword f = new frmPassword();
                f.ShowDialog();

                Application.Restart();
               
            }
            finally
            {
                con.Close();
            }
        }

        public static DataSet GetTable(string Query)
        {
            ds = new DataSet();
            ada.SelectCommand = new SqlCommand(Query, con);
            ada.Fill(ds);
            return ds;
        }

        public static object QueryRun(string Query)
        {
            cmd.Connection = con;
            cmd.CommandText = Query;
            return cmd.ExecuteScalar();
        }

        public string DataSource
        {
            get
            {
                return DataSource_;
            }
        }
        public string InitialCatalog
        {
            get
            {
                return InitialCatalog_;
            }
        }
        public string UserId
        {
            get
            {
                return UserId_;
            }
        }
        public string Password
        {
            get
            {
                return Password_;
            }
        }
        public static string ConnectionString
        {
            get
            {
                return ConnectionString_;
            }
        }
        
    }
}
