示例#1
0
文件: OleDb.cs 项目: pczy/Pub.Class
 /// <summary>
 /// Oracle大数据复制
 /// </summary>
 /// <param name="dt">数据源 dt.TableName一定要和数据库表名对应</param>
 /// <param name="dbkey">数据库</param>
 /// <param name="options">选项 默认Default</param>
 /// <param name="isTran">是否使用事务 默认false</param>
 /// <param name="timeout">超时时间7200 2小时</param>
 /// <param name="batchSize">每一批次中的行数</param>
 /// <param name="error">错误处理</param>
 /// <returns>true/false</returns>
 public bool DataBulkCopy(DataTable dt, string dbkey = "", BulkCopyOptions options = BulkCopyOptions.Default, bool isTran = false, int timeout = 7200, int batchSize = 10000, Action<Exception> error = null) {
     if (Data.Pool(dbkey).DBType != "OleDb") return false;
     using (OleDbConnection connection = new OleDbConnection(Data.Pool(dbkey).ConnString)) {
         connection.Open();
         OleDbDataAdapter adapter = new OleDbDataAdapter("select * from " + dt.TableName + "  where 1=0", connection);
         OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
         int rowcount = dt.Rows.Count;
         for (int n = 0; n < rowcount; n++) {
             dt.Rows[n].SetAdded();
         }
         adapter.UpdateBatchSize = batchSize;
         try {
             adapter.Update(dt);
         } catch(Exception ex) {
             if (error.IsNotNull()) error(ex);
             return false;
         }
     }
     return true;
 }
示例#2
0
 /// <summary>
 /// 重试方法
 /// </summary>
 /// <param name="action">方法</param>
 /// <param name="numRetries">重试次数</param>
 /// <param name="retryTimeout">延时多长时间后重试,单位毫秒</param>
 /// <param name="throwIfFail">经过几轮重试操作后依然发生异常时是否将异常抛出</param>
 /// <param name="onFailureAction">操作失败执行的方法</param>
 /// <returns></returns>
 public static void Retry(this Action action, int numRetries, int retryTimeout, bool throwIfFail, Action<Exception> onFailureAction) {
     if (action.IsNull()) throw new ArgumentNullException("action");
     numRetries--;
     do {
         bool istrue = false;
         try {
             action();
             istrue = true;
         } catch (Exception ex) {
             istrue = false;
             if (onFailureAction.IsNotNull()) onFailureAction(ex);
             if (numRetries <= 0 && throwIfFail) throw ex;
         }
         if (retryTimeout > 0 && !istrue) Thread.Sleep(retryTimeout);
     } while (numRetries-- > 0);
 }
示例#3
0
        /// <summary>
        /// 按宽X长比例切图 并补白
        /// </summary>
        /// <param name="imagePath">源图地址</param>
        /// <param name="savePath">新图地址</param>
        /// <param name="newWidth">宽度</param>
        /// <param name="newHeight">高度</param>
        /// <param name="error">出错时执行</param>
        /// <param name="bgcolor">填充背景色</param>
        /// <returns>成功true失败false</returns>
        public static bool CutImageByWidthHeight(string imagePath, string savePath, int newWidth, int newHeight, Color bgcolor, Action<Exception> error = null) {
            //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)  
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imagePath);
            int ow = originalImage.Width;//原始宽度  
            int oh = originalImage.Height;//原始高度  
            Size toSize = ResizeSite(new Size(newWidth, newHeight), new Size(ow, oh));
            int towidth = toSize.Width;//原图片缩放后的宽度  
            int toheight = toSize.Height;//原图片缩放后的高度  
            int x = 0;
            int y = 0;
            x = (newWidth - towidth) / 2;
            y = (newHeight - toheight) / 2;
            //新建一个bmp图片  
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一个画板  
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法  
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //设置高质量,低速度呈现平滑程度  
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充  
            g.Clear(bgcolor);
            //在指定位置并且按指定大小绘制原图片的指定部分  
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(0, 0, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);
            //--------------------------------------------------------------------------------  
            //新建一个bmp图片2  
            System.Drawing.Image bitmap2 = new System.Drawing.Bitmap(newWidth, newHeight);
            //新建一个画板2  
            System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(bitmap2);
            //设置高质量插值法  
            g2.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //设置高质量,低速度呈现平滑程度  
            g2.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充  
            g2.Clear(bgcolor);
            g2.DrawImageUnscaled(bitmap, new Point(x, y));

            try {
                bitmap2.Save(savePath, originalImage.RawFormat);
                return true;
            } catch (System.Exception ex) {
                if (error.IsNotNull()) error(ex);
                return false;
            } finally {
                originalImage.Dispose();
                bitmap.Dispose();
                bitmap2.Dispose();
                g2.Dispose();
                g.Dispose();
            }
        }
示例#4
0
        /// <summary>
        /// 按宽X长比例切图
        /// </summary>
        /// <param name="imagePath">源图地址</param>
        /// <param name="savePath">新图地址</param>
        /// <param name="cutWidth">宽度</param>
        /// <param name="cutHeight">高度</param>
        /// <param name="error">出错时执行</param>
        /// <returns>成功true失败false</returns>
        public static bool CutImageCustomMin(string imagePath, string savePath, int cutWidth, int cutHeight, Action<Exception> error = null) {
            try {
                System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
                float x = objImage.Width;
                float y = objImage.Height;
                float xPercent = x / cutWidth;
                float yPercent = y / cutHeight;

                int width = 0, height = 0;
                if (xPercent < yPercent) {
                    width = (int)((x * cutHeight) / y);
                    height = cutHeight;
                } else {
                    width = cutWidth;
                    height = (int)((cutWidth * y) / x);
                }

                System.Drawing.Image newimage = new Bitmap(objImage.Width, objImage.Height, PixelFormat.Format32bppRgb);
                Graphics g = Graphics.FromImage(newimage);
                g.DrawImage(objImage, 0, 0, objImage.Width, objImage.Height);
                g.Dispose();
                System.Drawing.Image thumbImage = newimage.GetThumbnailImage(width, height, null, IntPtr.Zero);
                thumbImage.Save(savePath, objImage.RawFormat);

                objImage.Dispose();
                newimage.Dispose();
                thumbImage.Dispose();

                FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                fs.Close();

                return true;
            } catch (Exception ex) {
                if (error.IsNotNull()) error(ex);
                return false;
            }
        }
示例#5
0
        /// <summary>
        /// 按宽X长比例切图
        /// </summary>
        /// <param name="imagePath">源图地址</param>
        /// <param name="savePath">新图地址</param>
        /// <param name="cutWidth">宽度</param>
        /// <param name="cutHeight">高度</param>
        /// <param name="error">出错时执行</param>
        /// <returns>成功true失败false</returns>
        public static bool CutImageCustom(string imagePath, string savePath, int cutWidth, int cutHeight, Action<Exception> error = null) {
            try {
                System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
                float x = objImage.Width;
                float y = objImage.Height;

                float xPercent = x / cutWidth;
                float yPercent = y / cutHeight;

                int width = 0, height = 0;
                if (xPercent < yPercent) {
                    width = (int)((x * cutHeight) / y);
                    height = cutHeight;
                } else {
                    width = cutWidth;
                    height = (int)((cutWidth * y) / x);
                }

                Bitmap newimage = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                newimage.SetResolution(72f, 72f);
                Graphics gdiobj = Graphics.FromImage(newimage);
                gdiobj.CompositingQuality = CompositingQuality.HighQuality;
                gdiobj.SmoothingMode = SmoothingMode.HighQuality;
                gdiobj.InterpolationMode = InterpolationMode.HighQualityBicubic;
#if !MONO40
                gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;
#endif
                gdiobj.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
                Rectangle destrect = new Rectangle(0, 0, width, height);
                gdiobj.DrawImage(objImage, destrect, 0, 0, objImage.Width, objImage.Height, GraphicsUnit.Pixel);
                gdiobj.Dispose();

                System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
                ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);

                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo ici = null;
                foreach (ImageCodecInfo codec in codecs) {
                    if (codec.MimeType == "image/jpeg") { ici = codec; }
                }

                if (ici.IsNotNull()) newimage.Save(savePath, ici, ep); else newimage.Save(savePath, objImage.RawFormat);

                objImage.Dispose();
                newimage.Dispose();

                FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                fs.Close();

                return true;
            } catch (Exception ex) {
                if (error.IsNotNull()) error(ex);
                return false;
            }
        }
示例#6
0
        /// <summary>
        /// 将图片剪切到一个正方形
        /// </summary>
        /// <param name="imagePath">源图地址</param>
        /// <param name="savePath">新图地址</param>
        /// <param name="square">正方形边长</param>
        /// <param name="error">出错时执行</param>
        /// <returns>成功true失败false</returns>
        public static bool CutImageSquare(string imagePath, string savePath, int square, Action<Exception> error = null) {
            try {
                int width = square;
                int height = square;
                int cutWidth = square;
                int cutHeight = square;

                System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);

                if (objImage.Width >= objImage.Height) {
                    cutWidth = objImage.Height;
                    cutHeight = objImage.Height;
                } else {
                    cutWidth = objImage.Width;
                    cutHeight = objImage.Width;
                }

                System.Drawing.Image newimage = new Bitmap(cutWidth, cutHeight, PixelFormat.Format32bppRgb);
                Graphics g = Graphics.FromImage(newimage);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                Rectangle destRect = new Rectangle(0, 0, cutWidth, cutHeight);
                Rectangle srcRect = new Rectangle(0, 0, cutWidth, cutHeight);
                GraphicsUnit units = GraphicsUnit.Pixel;

                g.DrawImage(objImage, destRect, srcRect, units);
                g.Dispose();
                System.Drawing.Image thumbImage = newimage.GetThumbnailImage(width, height, null, IntPtr.Zero);
                thumbImage.Save(savePath, objImage.RawFormat);

                objImage.Dispose();
                newimage.Dispose();
                thumbImage.Dispose();

                FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                fs.Close();

                return true;
            } catch (Exception ex) {
                if (error.IsNotNull()) error(ex);
                return false;
            }
        }
        public static void ForEachFileRecuresive(this DirectoryInfo directoryInfo, string pattern, Action<FileInfo> actionOnFile, Action<DirectoryInfo> actionOnDirectory = null)
        {
            if (!directoryInfo.Exists)
            {
                throw new DirectoryNotFoundException(directoryInfo.FullName);
            }
            if (actionOnDirectory.IsNotNull())
            {
                actionOnDirectory(directoryInfo);
            }
            var subDirectories = directoryInfo.GetDirectories();
            foreach (var subDirectory in subDirectories)
            {
                ForEachFileRecuresive(subDirectory, pattern, actionOnFile,actionOnDirectory);
            }

            var files = directoryInfo.GetFiles(pattern);
            foreach (var fileInfo in files)
            {
                actionOnFile(fileInfo);
            }
        }
示例#8
0
 /// <summary>
 /// SqlServer大数据复制
 /// </summary>
 /// <param name="dr">数据源</param>
 /// <param name="tableName">对应的表名</param>
 /// <param name="dbkey">数据库</param>
 /// <param name="options">选项 默认Default</param>
 /// <param name="isTran">是否使用事务 默认false</param>
 /// <param name="timeout">超时时间7200 2小时</param>
 /// <param name="batchSize">每一批次中的行数</param>
 /// <param name="error">错误处理</param>
 /// <returns>true/false</returns>
 public bool DataBulkCopy(IDataReader dr, string tableName, string dbkey = "", BulkCopyOptions options = BulkCopyOptions.Default, bool isTran = false, int timeout = 7200, int batchSize = 10000, Action<Exception> error = null) {
     if (Data.Pool(dbkey).DBType != "SqlServer") return false;
     SqlTransaction tran = null;
     using(SqlConnection conn = new SqlConnection(Data.Pool(dbkey).ConnString)) {
         if (isTran) tran = conn.BeginTransaction();
         using (System.Data.SqlClient.SqlBulkCopy bc = new System.Data.SqlClient.SqlBulkCopy(conn, ((int)options).ToEnum<SqlBulkCopyOptions>(), tran)) {
             bc.BulkCopyTimeout = timeout;
             bc.BatchSize = batchSize;
             bc.DestinationTableName = tableName;
             try {
                 bc.WriteToServer(dr);
                 if (isTran) tran.Commit();
             } catch(Exception ex) {
                 if (isTran) tran.Rollback();
                 if (error.IsNotNull()) error(ex);
                 return false;
             }
         }
     }
     return true;
 }
示例#9
0
 /// <summary>
 /// 大数据复制
 /// </summary>
 /// <param name="conn">连接源</param>
 /// <param name="tran">事务</param>
 /// <param name="dt">数据源 dt.TableName一定要和数据库表名对应</param>
 /// <param name="options">选项 默认Default</param>
 /// <param name="timeout">超时时间7200 2小时</param>
 /// <param name="batchSize">每一批次中的行数</param>
 /// <param name="error">错误处理</param>
 /// <returns></returns>
 public bool DataBulkCopy(IDbConnection conn, IDbTransaction tran, DataTable dt, BulkCopyOptions options = BulkCopyOptions.Default, int timeout = 7200, int batchSize = 10000, Action<Exception> error = null) {
     using (System.Data.SqlClient.SqlBulkCopy bc = new System.Data.SqlClient.SqlBulkCopy((SqlConnection)conn, ((int)options).ToEnum<SqlBulkCopyOptions>(), (SqlTransaction)tran)) {
         bc.BulkCopyTimeout = timeout;
         bc.BatchSize = batchSize;
         bc.DestinationTableName = dt.TableName;
         try {
             bc.WriteToServer(dt);
         } catch(Exception ex) {
             if (error.IsNotNull()) error(ex);
             return false;
         }
     }
     return true;
 }
示例#10
0
 /// <summary>
 /// 定时器
 /// </summary>
 /// <param name="run">执行函数</param>
 /// <param name="interval">计时器 1000为1秒</param>
 /// <param name="degree">跌代次数</param>
 /// <param name="error">连续返回false时执行</param>
 private static void setTimeout(Func<int, bool> run, int interval = 1000, int degree = 60, Action error = null) {
     int _degree = 0;
     System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
     Action end = () => {
         timer.Enabled = false;
         timer.Dispose();
     };
     timer.Interval = interval;
     timer.Enabled = true;
     timer.Tick += (s, e) => {
         if (degree < 1) {
             _degree++;
             if (run(_degree)) end();
         } else {
             _degree++;
             if (_degree >= degree) {
                 end();
                 if (!run(_degree) && error.IsNotNull()) error();
             } else if (run(_degree)) end();
         }
     };
 }
 public void RaiseInterceptionAspect(Action action)
 {
     if (action.IsNotNull()) {
         action();
     }
 }