コード例 #1
0
 public void AddOrUpdate(string environment, MereDataSource mereDataSource)
 {
     if (Environments.ContainsKey(environment))
     {
         Environments[environment] = mereDataSource;
     }
     else
     {
         Environments.Add(environment, mereDataSource);
     }
 }
コード例 #2
0
        public static bool MereBulkCopy <T>(this IEnumerable <T> toCopyFrom, MereDataSource copyToMereDataSource, int batchSize = 1000, bool truncateLength = false)
            where T : new()
        {
            var toInsertList = toCopyFrom.ToList();

            if (toInsertList.Count <= 0)
            {
                return(true);
            }

            var mereTableMin = MereUtils.CacheCheck <T>();

            var mereDataReader = new MereDataReader <T>(toInsertList, truncateLength);

            using (var conn = MereUtils.GetConnection(copyToMereDataSource))
            {
                conn.Open();
                using (var cpy = new SqlBulkCopy(conn))
                {
                    cpy.EnableStreaming = true;
                    cpy.BulkCopyTimeout = 0;
                    cpy.BatchSize       = batchSize;
                    foreach (var mereColumn in mereTableMin.SelectMereColumns)
                    {
                        var col = mereColumn.ColumnName;
                        cpy.ColumnMappings.Add(col, col);
                    }
                    cpy.DestinationTableName = copyToMereDataSource.TableName ?? mereTableMin.TableName;
                    //cpy.NotifyAfter = 1;
                    //cpy.SqlRowsCopied += (o, e) => Console.WriteLine(e.RowsCopied);
                    cpy.WriteToServer(mereDataReader);
                }
            }

            return(true);
        }
コード例 #3
0
        public static async Task <bool> ExecuteAsync <T>(T src, MereDataSource mereDataSource) where T : new()
        {
            var mereTable = MereUtils.CacheCheck <T>();

            using (var myCn = MereUtils.GetConnection <T>(mereDataSource))
            {
                await myCn.OpenAsync();

                using (var tx = myCn.BeginTransaction())
                {
                    using (var cmd = myCn.CreateCommand())
                    {
                        cmd.Transaction = tx;

                        foreach (var sel in mereTable.SelectMereColumnsNoIdentity().Select((prop, index) => new { index, prop }))
                        {
                            var v = sel.prop.GetBase(src);
                            if (v is DateTime && DateTime.Parse(v.ToString()).Year < 1753)
                            {
                                v = new DateTime(1753, 1, 1);
                            }

                            cmd.Parameters.AddWithValue("@" + sel.prop.ColumnName, v ?? DBNull.Value); // Changed .PropertyName to .ColumnName
                        }

                        var key = mereTable.SelectMereColumns.FirstOrDefault(w => w.IsKey);

                        var identity = mereTable.SelectMereColumns.FirstOrDefault(w => w.IsIdentity);
                        if (key != null)
                        {
                            var val = key.GetBase(src);
                            cmd.Parameters.AddWithValue("@key", val);
                            cmd.CommandText = mereTable.GetUpsertSqlWithKey();

                            if (identity != null)
                            {
                                var inserted = await cmd.ExecuteNonQueryAsync();

                                cmd.CommandText = "SELECT @@IDENTITY";
                                var reader = await cmd.ExecuteReaderAsync();

                                while (reader.Read())
                                {
                                    var id = reader[0];
                                    if (id != DBNull.Value)
                                    {
                                        identity.SetBase(src, id);
                                    }
                                }
                                reader.Close();
                                tx.Commit();

                                return(inserted > 0);
                            }
                        }

                        //defaults to insert if no key available
                        cmd.CommandText = mereTable.SqlInsert;
                        var inserted1 = await cmd.ExecuteNonQueryAsync();

                        tx.Commit();
                        if (inserted1 <= 0)
                        {
                        }
                        return(inserted1 > 0);
                    }
                }
            }
        }
コード例 #4
0
ファイル: MereDelete.cs プロジェクト: ksc4130/Mere
 public static IMereDeletePre <T> Create <T>(MereDataSource dataSource) where T : new()
 {
     return(MereDelete <T> .Create(dataSource));
 }
コード例 #5
0
 public static async Task <bool> MereSaveAsync <T>(this T src, MereDataSource mereDataSource) where T : new()
 {
     return(await Mere.MereSave.ExecuteAsync(src, mereDataSource));
 }
コード例 #6
0
 public static bool MereSave <T>(this T src, MereDataSource mereDataSource) where T : new()
 {
     return(Mere.MereSave.Execute(src, mereDataSource));
 }
コード例 #7
0
 public static Task <bool> MereBulkInsertAsync <T>(this IList <T> toInsertEn, int batchSize, MereDataSource mereDataSource) where T : new()
 {
     return(toInsertEn.MereBulkInsertAsync <T>(batchSize, false, mereDataSource));
 }
コード例 #8
0
        public async static Task <bool> MereBulkInsertAsync <T>(this IEnumerable <T> toInsertEn, int batchSize, bool truncateLength, MereDataSource mereDataSource) where T : new()
        {
            var toInsertList = toInsertEn.ToList();

            if (toInsertList.Count <= 0)
            {
                return(true);
            }

            var           mereTableMin   = MereUtils.CacheCheck <T>();
            var           mereDataReader = new MereDataReader <T>(toInsertList, truncateLength);
            SqlConnection conn           = mereDataSource == null?MereUtils.GetConnection <T>() : MereUtils.GetConnection(mereDataSource);

            using (conn)
            {
                await conn.OpenAsync();

                using (var cpy = new SqlBulkCopy(conn))
                {
                    cpy.EnableStreaming = true;
                    cpy.BulkCopyTimeout = 0;
                    cpy.BatchSize       = batchSize;

                    mereTableMin.SelectMereColumns.ForEach(
                        x => cpy.ColumnMappings.Add(x.ColumnName, x.ColumnName));

                    cpy.DestinationTableName = mereTableMin.TableName;

                    await cpy.WriteToServerAsync(mereDataReader);
                }
            }

            return(true);
        }
コード例 #9
0
 public static IMereQueryPre <T> Create <T>(MereDataSource dataSource, int top) where T : new()
 {
     return(MereQuery <T> .Create(dataSource, top));
 }
コード例 #10
0
 public static MereInsert <T> Create <T>(MereDataSource mds) where T : new()
 {
     return(MereInsert <T> .Create(mds));
 }