Example #1
0
        public async Task <int> InsertAsync <T>(T entity) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Insert(entityInfo);

            var parameters = new MyDbParameters();

            parameters.Add(entity);

            var command = new SqlCommand(sql);

            command.Parameters.AddRange(parameters.Parameters);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                var obj = await command.ExecuteScalarAsync();

                if (obj != DBNull.Value)
                {
                    entity.Id = Convert.ToInt32(obj);
                }
                return(entity.Id);
            }
        }
Example #2
0
 public MySelect(string connectionString, string fields, string table, string where, MyDbParameters dbParameters, string orderBy)
 {
     _fields           = fields;
     _table            = table;
     _where            = where;
     _parameters       = dbParameters;
     _orderBy          = orderBy;
     _connectionString = connectionString;
 }
Example #3
0
        public async Task <int> InsertAsync <T>(List <T> entityList) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Insert(entityInfo);

            var count = 0;

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (var entity in entityList)
                        {
                            using (var command = new SqlCommand(sql, conn, trans))
                            {
                                var parameters = new MyDbParameters();
                                parameters.Add(entity);
                                command.Parameters.AddRange(parameters.Parameters);
                                var obj = await command.ExecuteScalarAsync();

                                if (obj != DBNull.Value)
                                {
                                    entity.Id = Convert.ToInt32(obj);
                                    count++;
                                }
                            }
                        }
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        count = 0;
                    }
                }
            }

            return(count);
        }
Example #4
0
        public async Task <int> UpdateAsync <T>(T entity) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Update(entityInfo, "");

            var parameters = new MyDbParameters();

            parameters.Add(entity);

            var command = new SqlCommand(sql);

            command.Parameters.AddRange(parameters.Parameters);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                return(await command.ExecuteNonQueryAsync());
            }
        }
Example #5
0
        /// <summary>
        /// 更新多个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityList">要更新的实体列表</param>
        /// <returns>受影响的记录数</returns>
        public int Update <T>(List <T> entityList) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Update(entityInfo, "");

            var count = 0;

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (var entity in entityList)
                        {
                            using (var command = new SqlCommand(sql, conn, trans))
                            {
                                var param = new MyDbParameters();
                                param.Add(entity);
                                command.Parameters.AddRange(param.Parameters);
                                count += command.ExecuteNonQuery();
                            }
                        }
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                        count = 0;
                    }
                }
            }

            return(count);
        }
        private static MyDbParameters ConvertToDbParameter(object obj)
        {
            var myDbParameters = new MyDbParameters();

            if (obj is SqlParameter parameter)
            {
                myDbParameters.Add(parameter);
            }
            else if (obj is IEnumerable <SqlParameter> enumerable)
            {
                myDbParameters.Add(enumerable);
            }
            else if (obj is MyDbParameters dbParameters)
            {
                myDbParameters.Add(dbParameters.Parameters);
            }
            else
            {
                myDbParameters.Add(obj);
            }

            return(myDbParameters);
        }