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

namespace Publishmentation
{
	/// <summary>
	/// Summary description for Connector.
	/// </summary>
	public class Connector
	{
        public SqlConnection myConnection = new SqlConnection("server=(local);database=Publishmentation;Integrated Security=true");
		private SqlCommand myCommand=new SqlCommand("select *From M_User");
		private SqlDataAdapter myAdapter=new SqlDataAdapter();
		public DataSet myDataSet=new DataSet();
		private SqlDataReader myReader;
		public string userName;
//**************************************************************************************		
		public Connector()
		{
			myCommand.Connection=myConnection;
			myAdapter.SelectCommand=myCommand;
		}
//---------------------------------------------------------------------------------------
		public Connector(string ConnectionString)
		{
			myConnection.ConnectionString=ConnectionString;
		}
//**************************************************************************************		
		
		public void connect()
		{
			myConnection.Open();
		}
//---------------------------------------------------------------------------------------
		
		public void disConnect()
		{
			myConnection.Close();
		}
//---------------------------------------------------------------------------------------
   
		public SqlDataReader select(string selectCommand)
		{
            myCommand.CommandText=selectCommand;
			return myReader=myCommand.ExecuteReader();
		}
//---------------------------------------------------------------------------------------

		public DataSet selecte(string selectCommand)
		{
			myCommand.CommandText=selectCommand;
			myAdapter.SelectCommand=myCommand;
			myAdapter.Fill(myDataSet);
			return myDataSet;
		}
//---------------------------------------------------------------------------------------

		public void updateCommand(string selectCommand)
		{
            try
            {
                connect();
                myCommand.CommandText = selectCommand;
                myCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("!ارتباط با بانک اطلاعاتی با مشکل روبرو شده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                disConnect();
            }
		}
//---------------------------------------------------------------------------------------
        public bool storeProcedure(SqlCommand command)
        {
            try
            {
                connect();
                command.Connection = myConnection;
                command.ExecuteNonQuery();
                return true;
            }
            catch (SqlException e)
            {
                MessageBox.Show("!ارتباط با بانک اطلاعاتی با مشکل روبرو شده است",e.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            finally 
            {
                disConnect();
            }
        }
    public int storeProcedure2(SqlCommand command)
        {
            try
            {
                connect();
                command.Connection = myConnection;
                return  Convert.ToInt32(command.ExecuteScalar());
            }
            catch (SqlException e)
            {
                MessageBox.Show("!ارتباط با بانک اطلاعاتی با مشکل روبرو شده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                myDataSet = null;
                return 0;
            }
            finally 
            {
                disConnect();
            }
        }
	//-------------------------------------------------------------
        public DataSet storeProcedure3(SqlCommand command)
        {
            try
            {
                connect();
                command.Connection = myConnection;
                myAdapter.SelectCommand = command;
                myAdapter.Fill(myDataSet,"Report");
                return myDataSet;
            }
            catch (SqlException e)
            {
                MessageBox.Show("!ارتباط با بانک اطلاعاتی با مشکل روبرو شده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                myDataSet = null;
                return myDataSet;
            }
            finally
            {
                disConnect();
            }
        }

    }
}
