public bool Insert <T>(T t) where T : BaseModel, new() { if (!t.ValidataModel()) { throw new Exception("数据验证失败"); } Type type = typeof(T); //string columnStrings = string.Join(",", type.GetPropertiesWithNoKey().Select(p => $"[{p.GetMappingName()}]")); //string valueStrings = string.Join(",", type.GetPropertiesWithNoKey().Select(p => $"@{p.GetMappingName()}")); // 泛型缓存 //string sql = $"insert into [{type.Name}] ({columnStrings}) values ({valueStrings})"; string sql = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.Insert); //参数化 var sqlParameters = type.GetProperties().Select(p => new SqlParameter($"@{p.GetMappingName()}", p.GetValue(t) ?? DBNull.Value)).ToArray(); return(this.ExceuteSql <bool>(sql, sqlParameters, sqlCommand => 1 == sqlCommand.ExecuteNonQuery())); //using(SqlConnection connection = new SqlConnection(configuationmanager)) //{ // SqlCommand command = new SqlCommand(sql, connection); // command.Parameters.AddRange(parameters); // connection.Open(); // int result = command.ExecuteNonQuery(); // return result == 1; //} }
//private static string configuationmanager = ConfigurationManager.SqlConnectionStringWrite; /// <summary> /// 泛型完成通类 /// </summary> /// <param name="id"></param> /// <returns></returns> public T Find <T>(int id) where T : BaseModel, new() { Type type = typeof(T); //string colunmString = string.Join(",", type.GetProperties().Select(p => $"[{ p.GetMappingName()}]")); //string sql = $@"SELECT {colunmString} FROM [{type.GetMappingName()}] where id =@id"; SqlParameter[] sqlParameters = new SqlParameter[] { new SqlParameter("@id", id) }; string sql = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.FindOne); return(this.ExceuteSql <T>(sql, sqlParameters, sqlCommand => { var reader = sqlCommand.ExecuteReader(); if (reader.Read()) { T t = Activator.CreateInstance <T>(); foreach (var prop in type.GetProperties()) { prop.SetValue(t, reader[prop.GetMappingName()] is DBNull ? null : reader[prop.GetMappingName()]); } return t; } else { return null; } }, SqlConnectionStringPool.DBOperateType.Read)); //using (SqlConnection connection = new SqlConnection(configuationmanager)) //{ // var command = connection.CreateCommand(); // SqlParameter[] para = new SqlParameter[1]; // para[0] = new SqlParameter("@id", id); // command.CommandText = sql; // command.Parameters.AddRange(para); // connection.Open(); // var reader = command.ExecuteReader(); // if (reader.Read()) // { // T t = Activator.CreateInstance<T>(); // foreach(var prop in type.GetProperties()) // { // prop.SetValue(t, reader[prop.GetMappingName()] is DBNull? null: reader[prop.GetMappingName()]); // } // return t; // } //} }
public bool Delete <T>(T t) where T : BaseModel, new() { Type type = typeof(T); //string sql = $"Delete from [{type.GetMappingName()}] where Id=@id"; string sql = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.Delete); SqlParameter[] sqlParameters = new SqlParameter[] { new SqlParameter("@id", t.Id) }; return(this.ExceuteSql <bool>(sql, sqlParameters, sqlCommand => 1 == sqlCommand.ExecuteNonQuery())); //using (SqlConnection connection = new SqlConnection(configuationmanager)) //{ // SqlCommand command = new SqlCommand(sql, connection); // command.Parameters.AddRange(sqlParameters); // connection.Open(); // int result = command.ExecuteNonQuery(); // return result == 1; //} }