Example #1
0
        /// <summary>
        /// 在数据库事务中执行SQL语句返回影响行数
        /// </summary>
        /// <param name="sqls">SQL语句</param>
        public void ExecuteSQLWithTransaction(SqlStatementCollection sqls)
        {
            if (sqls == null)
            {
                throw new ArgumentNullException("DatabaseImpl.ExecuteSQLWithTransaction.sqls");
            }

            var sqlGroups = sqls.GroupBy(i => i.SqlBuildingInfo.DataSource);

            using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
            {
                IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted
            }))
                try
                {
                    foreach (var sqlGroup in sqlGroups)
                    {
                        if (sqlGroup.Count() == 0)
                        {
                            continue;
                        }
                        var dataSourceName = sqlGroup.FirstOrDefault().SqlBuildingInfo.DataSource;

                        using (var db = GetDatabase(dataSourceName))
                        {
                            foreach (var sqlstatment in sqlGroup)
                            {
                                var sqlString = sqlstatment.ToSQL();
                                try
                                {
                                    var parameters = new List <IDbDataParameter>();
                                    parameters = CreateParameters(db, sqlstatment, parameters);

                                    if (parameters == null || parameters.Count == 0)
                                    {
                                        db.ExecSqlStatement(sqlString);
                                    }
                                    else
                                    {
                                        db.ExecSqlStatement(sqlString, parameters.ToArray());
                                    }
                                }
                                catch (Exception e)
                                {
                                    MonitorError(e, sqlString);
                                    throw;
                                }
                            }
                        }
                    }

                    ts.Complete();
                }
                catch (Exception e)
                {
                    MonitorError(e, new string[sqls.Count]);
                    throw;
                }
        }
Example #2
0
        /// <summary>
        /// 执行SQL获取数据
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>数据</returns>
        public List <DataTable> GetDataCollection(SqlStatementCollection sqls)
        {
            if (sqls == null)
            {
                throw new ArgumentNullException("DatabaseImpl.GetData.sqls");
            }

            var dataTables = new List <DataTable>();
            var sqlGroups  = sqls.GroupBy(i => i.SqlBuildingInfo.DataSource);

            try
            {
                foreach (var sqlGroup in sqlGroups)
                {
                    if (sqlGroup.Count() == 0)
                    {
                        continue;
                    }
                    var dataSourceName = sqlGroup.FirstOrDefault().SqlBuildingInfo.DataSource;

                    using (var db = GetDatabase(dataSourceName))
                    {
                        foreach (var sqlstatment in sqlGroup)
                        {
                            var sqlString = sqlstatment.ToSQL();
                            try
                            {
                                var parameters = new List <IDbDataParameter>();

                                if (sqlstatment is SelectSqlStatement)
                                {
                                    parameters = ParameterHandlerForSelect(sqlstatment, db);
                                }

                                var ds = new DataSet();
                                if (parameters == null || parameters.Count == 0)
                                {
                                    ds = db.ExecuteDataSet(sqlString);
                                }
                                else
                                {
                                    ds = db.ExecuteDataSet(sqlString, parameters.ToArray());
                                }

                                if (ds != null)
                                {
                                    foreach (DataTable dt in ds.Tables)
                                    {
                                        dataTables.Add(dt);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                MonitorError(e, sqlString);
                                throw;
                            }
                        }
                    }
                }

                return(dataTables);
            }
            catch (Exception e)
            {
                MonitorError(e, new string[sqls.Count]);
                throw;
            }
        }