Пример #1
0
        public void Insert()
        {
            var dapperInstance = _dapper;

            #region 实体对象赋值
            var entity = new DapperTestEntity()
            {
                bigint_value         = 1,
                bit_value            = true,
                bool_value           = true,
                boolean_value        = true,
                date_value           = DateTime.Now.Date,
                char_null_value      = null,
                dec_value            = 1,
                decimal_value        = 1,
                double_value         = 1,
                fix_value            = 1,
                float_value          = 1,
                int_value            = 1,
                integer_value        = 1,
                longtext_null_value  = null,
                mediumint_value      = 1,
                mediumint_null_value = null,
                nchar_null_value     = null,
                nvarchar_null_value  = null,
                numeric_value        = 1,
                real_value           = 1,
                smallint_value       = 1,
                text_null_value      = "",
                tinytext_null_value  = null,
                tinyint_value        = 1,
                varbinary_null_value = new byte[] { 0x01, 0x02, 0x03 },
                binary_null_value    = new byte[] { 0x01, 0x02, 0x03 },
                id = 1
            };
            #endregion

            try
            {
                ClearDapperTestEntity();

                //Dapper插入对象
                dapperInstance.Insert(entity);
                string sql = string.Format("select * from dapper_all_test where id = '{0}'", entity.id);
                helper.ExecuteReader("select * from dapper_all_test where id=1", reader =>
                {
                    while (reader.Read())
                    {
                        CompareHelper.Compare(reader, entity);
                    }
                });
            }
            finally
            {
                ClearDapperTestEntity();
            }
        }
Пример #2
0
        private DapperTestEntity CreateDapperTestEntity(int id = 1)
        {
            var entity = new DapperTestEntity
            {
                bigint_value         = 1,
                bit_value            = true,
                bool_value           = true,
                boolean_value        = true,
                date_value           = DateTime.Now,
                char_null_value      = null,
                dec_value            = 1,
                decimal_value        = 1,
                double_value         = 1,
                fix_value            = 1,
                float_value          = 1,
                int_value            = 1,
                integer_value        = 1,
                longtext_null_value  = null,
                mediumint_null_value = null,
                nchar_null_value     = null,
                nvarchar_null_value  = null,
                mediumint_value      = 1,
                numeric_value        = 1,
                real_value           = 1,
                tinytext_null_value  = null,
                smallint_value       = 1,
                text_null_value      = "",
                tinyint_value        = 1,
                id             = id,
                datetime_value = new DateTime(2016, 12, 12, 4, 4, 4)
            };
            var sql = string.Format(@"
                        insert into dapper_all_test(
                        bigint_value,bit_value,bool_value,boolean_value,date_value,dec_value,decimal_value,double_value,fix_value,float_value,int_value,integer_value,
                        mediumint_value,numeric_value,real_value,smallint_value,text_null_value,tinyint_value,id,datetime_value) 
                        value ({0},{1},{2},{3},'{4}','{5}',{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},'{16}',{17},{18},'{19}')",
                                    entity.bigint_value, entity.bit_value == true ? 1 : 0, entity.bool_value == true ? 1 : 0, entity.boolean_value == true ? 1 : 0, entity.date_value.ToString("yyyy-MM-dd HH:mm:ss"), entity.dec_value,
                                    entity.decimal_value, entity.double_value, entity.fix_value, entity.float_value, entity.int_value, entity.integer_value, entity.mediumint_value, entity.numeric_value, entity.real_value, entity.smallint_value, entity.text_null_value
                                    , entity.tinyint_value, entity.id, entity.datetime_value.ToString("yyyy-MM-dd HH:mm:ss"));

            if (helper.ExecuteSQL(sql, cmd => cmd.ExecuteNonQuery()) > 0)
            {
                return(entity);
            }
            return(null);
        }
Пример #3
0
 public static void Compare(DbDataReader reader, DapperTestEntity entity)
 {
     reader.ColumnValueCompare("id", Convert.ToInt32, entity.id)
     .ColumnValueCompare("bigint_value", entity.bigint_value)
     .ColumnValueCompare("bigint_null_value", entity.bigint_null_value)
     .ColumnValueCompare("bit_value", Convert.ToBoolean, entity.bit_value)
     .ColumnValueCompare("bit_null_value", entity.bit_null_value)
     .ColumnValueCompare("bool_value", entity.bool_value)
     .ColumnValueCompare("bool_null_value", entity.bool_null_value)
     .ColumnValueCompare("boolean_value", entity.boolean_value)
     .ColumnValueCompare("boolean_null_value", entity.boolean_null_value)
     .ColumnValueCompare("char_null_value", entity.char_null_value)
     .ColumnValueCompare("date_value", dt => dt == null ? null : Convert.ToDateTime(dt).ToString("yyyy-MM-dd"), entity.date_value.ToString("yyyy-MM-dd"))
     .ColumnValueCompare("date_null_value", entity.date_null_value == null ? string.Empty : entity.date_null_value.Value.ToString("yyyy-MM-dd"))
     .ColumnValueCompare("datetime_value", entity.datetime_value)
     .ColumnValueCompare("datetime_null_value", entity.datetime_null_value)
     .ColumnValueCompare("dec_value", entity.dec_value)
     .ColumnValueCompare("dec_null_value", entity.dec_null_value)
     .ColumnValueCompare("decimal_value", entity.decimal_value)
     .ColumnValueCompare("decimal_null_value", entity.decimal_null_value)
     .ColumnValueCompare("double_value", entity.double_value)
     .ColumnValueCompare("double_null_value", entity.double_null_value)
     .ColumnValueCompare("fix_value", entity.fix_value)
     .ColumnValueCompare("fix_null_value", entity.fix_null_value)
     .ColumnValueCompare("float_value", entity.float_value)
     .ColumnValueCompare("float_null_value", entity.float_null_value)
     .ColumnValueCompare("int_value", entity.int_value)
     .ColumnValueCompare("int_null_value", entity.int_null_value)
     .ColumnValueCompare("integer_value", entity.integer_value)
     .ColumnValueCompare("integer_null_value", entity.integer_null_value)
     .ColumnValueCompare("longtext_null_value", entity.longtext_null_value)
     .ColumnValueCompare("mediumint_value", entity.mediumint_value)
     .ColumnValueCompare("mediumint_null_value", entity.mediumint_null_value)
     .ColumnValueCompare("mediumtext_null_value", entity.mediumtext_null_value)
     .ColumnValueCompare("nchar_null_value", entity.nchar_null_value)
     .ColumnValueCompare("numeric_value", entity.numeric_value)
     .ColumnValueCompare("numeric_null_value", entity.numeric_null_value)
     .ColumnValueCompare("nvarchar_null_value", entity.nvarchar_null_value)
     .ColumnValueCompare("real_value", entity.real_value)
     .ColumnValueCompare("real_null_value", entity.real_null_value)
     .ColumnValueCompare("smallint_value", entity.smallint_value)
     .ColumnValueCompare("smallint_null_value", entity.smallint_null_value)
     .ColumnValueCompare("text_null_value", entity.text_null_value)
     .ColumnValueCompare("tinyint_value", entity.tinyint_value)
     .ColumnValueCompare("tinyint_null_value", entity.tinyint_null_value)
     .ColumnValueCompare("tinytext_null_value", entity.tinytext_null_value)
     .ColumnValueCompare("varchar_null_value", entity.varchar_null_value ?? string.Empty);
     if (entity.binary_null_value == null)
     {
         reader.ColumnValueCompare("binary_null_value", string.Empty);
     }
     if (entity.varbinary_null_value != null)
     {
         reader.ColumnValueCompare("varbinary_null_value",
                                   bytes => System.Text.Encoding.Default.GetString((byte[])bytes),
                                   System.Text.Encoding.Default.GetString(entity.varbinary_null_value));
     }
     else
     {
         reader.ColumnValueCompare("varbinary_null_value", string.Empty);
     }
 }