Ejemplo n.º 1
0
        public ulong Insert(TEntity entity)
        {
            ulong         lastId     = 0;
            List <string> attributes = typeof(TEntity).GetProperties(BindingFlags.DeclaredOnly |
                                                                     BindingFlags.Public |
                                                                     BindingFlags.Instance).Where(p => !p.PropertyType.IsGenericType).Select(p => p.Name).ToList();

            string       sql = InsertSqlBuilder(typeof(TEntity).Name, attributes);
            MySqlCommand com = new MySqlCommand(sql, ConnectionString.GetConnection());

            try
            {
                foreach (string att in attributes)
                {
                    // if it is a datetime convert it
                    if (entity.GetType().GetProperty(att).PropertyType == typeof(DateTime))
                    {
                        string dateValue = FormatConverter
                                           .ConvertToMySqlDateString(
                            (DateTime)entity.GetType().GetProperty(att).GetValue(entity));
                        com.Parameters.Add(new MySqlParameter(
                                               att,
                                               dateValue));

                        continue;
                    }

                    // for ordinary data values
                    com.Parameters.Add(new MySqlParameter(
                                           att,
                                           entity.GetType().GetProperty(att).GetValue(entity).ToString()));
                }


                com.Connection.Open();

                lastId = (ulong)com.ExecuteScalar();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                com.Connection.Close();
            }
            return(lastId);
        }
Ejemplo n.º 2
0
        public int Update(TEntity entity)
        {
            try
            {
                //List<string> attributes = new List<string>() { "name", "address" };
                List <string> attributes = typeof(TEntity)
                                           .GetProperties(BindingFlags.FlattenHierarchy |
                                                          BindingFlags.Public |
                                                          BindingFlags.Instance)
                                           .Where(p => !p.PropertyType.IsGenericType)
                                           .Select(p => p.Name)
                                           .ToList();
                string sql = UpdateSqlBuilder(typeof(TEntity).Name, attributes);

                MySqlCommand com = new MySqlCommand(sql, ConnectionString.GetConnection());

                foreach (string att in attributes)
                {
                    // if it is a datetime convert it
                    if (entity.GetType().GetProperty(att).PropertyType == typeof(DateTime))
                    {
                        string dateValue = FormatConverter
                                           .ConvertToMySqlDateString(
                            (DateTime)entity.GetType().GetProperty(att).GetValue(entity));
                        com.Parameters.Add(new MySqlParameter(
                                               att,
                                               dateValue));

                        continue;
                    }

                    com.Parameters.Add(new MySqlParameter(
                                           att,                                                             // Parameter Name
                                           entity.GetType().GetProperty(att).GetValue(entity).ToString())); // Parameter Value
                }

                com.Connection.Open();

                return(com.ExecuteNonQuery());
            }
            catch (Exception e)
            {
                throw e;
            }
        }