예제 #1
0
 public static void Compare(DbDataReader reader, SQLiteDapperTestEntity entity)
 {
     reader.ColumnValueCompare("id", Convert.ToInt32, entity.id)
     .ColumnValueCompare("bigint_value", entity.bigint_value)
     .ColumnValueCompare("bigint_null_value", entity.bigint_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);
     }
 }
예제 #2
0
        private SQLiteDapperTestEntity CreateDapperTestEntity(int id = 1)
        {
            var entity = new SQLiteDapperTestEntity
            {
                id                  = id,
                bigint_value        = 2,
                datetime_value      = new DateTime(1988, 11, 12, 17, 34, 21),
                date_value          = new DateTime(1988, 11, 12),
                decimal_value       = 100m,
                dec_value           = 200m,
                double_value        = 300d,
                fix_value           = 500.5m,
                float_value         = 600.6f,
                integer_value       = 7654321,
                int_value           = 8888888,
                mediumint_value     = 999,
                numeric_value       = 10101,
                real_value          = 12312,
                smallint_value      = 23,
                tinyint_value       = 11,
                longtext_null_value = "codemonk"
            };
            var sql = string.Format($@"
                        insert into ""dapper_all_test""(
""id"",
""bigint_value"",
""datetime_value"",
""date_value"",
""decimal_value"",
""dec_value"",
""double_value"",
""fix_value"",
""float_value"",
""integer_value"",
""int_value"",
""mediumint_value"",
""numeric_value"",
""real_value"",
""smallint_value"",
""tinyint_value"", 
""longtext_null_value"") values (
{entity.id},{entity.bigint_value},'{entity.datetime_value}','{entity.datetime_value}',
{entity.decimal_value},{entity.dec_value},{entity.double_value},{entity.fix_value},{entity.float_value},{entity.integer_value}
,{entity.int_value},{entity.mediumint_value},{entity.numeric_value},{entity.real_value},{entity.smallint_value},{entity.tinyint_value},'{entity.longtext_null_value}')");

            if (SQLiteHelper.ExecuteSQL(sql, cmd => cmd.ExecuteNonQuery()) > 0)
            {
                return(entity);
            }
            return(null);
        }
예제 #3
0
        public void InsertSync()
        {
            var dapperInstance = _dapper;

            #region 实体对象赋值
            var entity = new SQLiteDapperTestEntity
            {
                id                  = 22,
                bigint_value        = 2,
                datetime_value      = new DateTime(1988, 11, 12, 17, 34, 21),
                date_value          = new DateTime(1988, 11, 12),
                decimal_value       = 100m,
                dec_value           = 20.3m,
                double_value        = 300d,
                fix_value           = 500.5m,
                float_value         = 610f,
                integer_value       = 76543,
                int_value           = 888,
                mediumint_value     = 999,
                numeric_value       = 10101,
                real_value          = 12312,
                smallint_value      = 23,
                tinyint_value       = 11,
                longtext_null_value = "codemonk"
            };
            #endregion

            try
            {
                ClearDapperTestEntity();

                var result = SQLiteHelper.GetConnection(x => x.Execute(@"insert into ""test_table""(""CREATE_TIME"")values(:ct)", new { ct = DateTime.UtcNow }));
                Assert.True(result > 0);


                //Dapper插入对象
                dapperInstance.InsertAsync(entity).GetAwaiter().GetResult();
                var sql = $@"select * from ""dapper_all_test"" where ""id"" = '{entity.id}'";
                SQLiteHelper.ExecuteReader(sql, reader =>
                {
                    while (reader.Read())
                    {
                        CompareHelper.Compare(reader, entity);
                    }
                });
            }
            finally
            {
                ClearDapperTestEntity();
            }
        }