Exemplo n.º 1
0
        // Execute Query
        private int ExecuteQuery(string query, ESqlQueries sqlQueries)
        {
            int           result     = 0;
            SqlConnection connection = null;
            SqlCommand    command    = null;

            if (query == null)
            {
                throw new Exception(string.Format(QUERY_NOT_FOUND, Enum.GetName(typeof(ESqlQueries), sqlQueries)));
            }
            try
            {
                connection = ConnectionManager.Get().GetConnection();
                command    = new SqlCommand(query, connection);
                connection.Open();
                result = command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(DATABASE_READING_ERROR, e);
            }
            finally
            {
                if ((connection != null) && (connection.State == System.Data.ConnectionState.Open))
                {
                    connection.Close();
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        // Execute Query
        private int ExecuteQuery(string query, ESqlQueries sqlQueries)
        {
            int           result     = 0;
            SqlConnection connection = null;
            SqlCommand    command    = null;

            //
            if (query == null)
            {
                // TODO Develop Custom Exception
                throw new Exception(string.Format(QUERY_NOT_FOUND, Enum.GetName(typeof(ESqlQueries), sqlQueries)));
            }
            try
            {
                connection = ConnectionManager.Get().GetConnection();
                command    = new SqlCommand(query, connection);
                connection.Open();
                result = command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                // TODO DEvelop Custom Exception
                Console.WriteLine(e.Message);
            }
            finally
            {
                if ((connection != null) && (connection.State == System.Data.ConnectionState.Open))
                {
                    connection.Close();
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        // Read
        private IList <TEntity> GetQueryResult(string query, ESqlQueries sqlQueries, SqlParameter[] Parameter = null)
        {
            IList <TEntity> all        = new List <TEntity>();
            SqlConnection   connection = null;
            SqlCommand      command    = null;
            SqlDataReader   reader     = null;
            IList <string>  queryResult;

            //
            if (query == null)
            {
                throw new QueryNotFoundExeption(Enum.GetName(typeof(ESqlQueries), sqlQueries));
            }
            try
            {
                connection = ConnectionManager.ConnectionManager.Get().GetConnection();
                command    = new SqlCommand(query, connection);
                ////////////////////????????????????????????????????????????
                if (Parameter != null)
                {
                    foreach (var param in Parameter)
                    {
                        command.Parameters.Add(param);
                    }
                }
                ////////////////////???????????????????????????????????????
                connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    queryResult = new List <string>();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        queryResult.Add(reader[i].ToString());
                    }
                    all.Add(CreateInstance(queryResult));
                }
            }
            catch (Exception e)
            {
                throw new DatabaseReadingExeption(e);
            }
            finally
            {
                if ((connection != null) && (connection.State == System.Data.ConnectionState.Open))
                {
                    connection.Close();
                }
                //?????????????????????????????????????????????????????????????????
                if (command != null)
                {
                    command.Dispose();
                }
            }
            //if (all.Count == 0)
            //{
            //    throw new EmptyDataReaderByQueryExeption(query);
            //}
            return(all);
        }
Exemplo n.º 4
0
        // Read
        private IList <TEntity> GetQueryResult(string query, ESqlQueries sqlQueries)
        {
            IList <TEntity> all        = new List <TEntity>();
            SqlConnection   connection = null;
            SqlCommand      command    = null;
            SqlDataReader   reader     = null;
            IList <string>  queryResult;

            //
            if (query == null)
            {
                // TODO Develop Custom Exception
                throw new Exception(string.Format(QUERY_NOT_FOUND, Enum.GetName(typeof(ESqlQueries), sqlQueries)));
            }
            try
            {
                connection = ConnectionManager.Get().GetConnection();
                command    = new SqlCommand(query, connection);
                connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    queryResult = new List <string>();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        queryResult.Add(reader[i].ToString());
                    }
                    all.Add(CreateInstance(queryResult));
                }
            }
            catch (Exception e)
            {
                // TODO DEvelop Custom Exception
                throw new Exception(DATABASE_READING_ERROR, e);
            }
            finally
            {
                if ((connection != null) && (connection.State == System.Data.ConnectionState.Open))
                {
                    connection.Close();
                }
            }
            if (all.Count == 0)
            {
                throw new Exception(string.Format(EMPTY_DATA_READER, query));
            }
            return(all);
        }
Exemplo n.º 5
0
        protected abstract IDictionary <Enum, (string field, string pvalue, dynamic value)> GetKeyFieldsValues(TEntity entity);    //EIEntity,("field","@value","value")

        // Execute Query
        private int ExecuteQuery(string query, ESqlQueries sqlQueries, List <SqlParameter> Parameters = null)
        {
            int           result     = 0;
            SqlConnection connection = null;
            SqlCommand    command    = null;

            //
            if (query == null)
            {
                throw new QueryNotFoundExeption(Enum.GetName(typeof(ESqlQueries), sqlQueries));
            }
            try
            {
                connection = ConnectionManager.ConnectionManager.Get().GetConnection();
                command    = new SqlCommand(query, connection);
                ////////////////////????????????????????????????????????????
                if (Parameters != null)
                {
                    foreach (var param in Parameters)
                    {
                        command.Parameters.Add(param);
                    }
                }
                ////////////////////???????????????????????????????????????
                connection.Open();
                result = command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new DatabaseReadingExeption(e);
            }
            finally
            {
                if ((connection != null) && (connection.State == System.Data.ConnectionState.Open))
                {
                    connection.Close();
                }
                ////////??????????????????????
                if (command != null)
                {
                    command.Dispose();
                }
            }
            return(result);
        }