http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

楼主: 站长苏飞

[数据库] 最新的C#SqlHelper 类苏飞修改版

  [复制链接]
发表于 2013-3-25 13:08:32 | 显示全部楼层
如果这个类有个使用的小DEMO例子就好了,我是新手


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-3-25 13:24:32 | 显示全部楼层
ttstudio 发表于 2013-3-25 13:08
如果这个类有个使用的小DEMO例子就好了,我是新手

http://www.sufeinet.com/forum.php?mod=viewthread&tid=28
 楼主| 发表于 2013-3-25 13:28:02 | 显示全部楼层
或者你直接So这个类名也可以找到不少例子
发表于 2013-4-23 02:43:08 | 显示全部楼层
这个类有点乱,最好把常用的精简一下
比如这样:
        public static readonly string ConnectionString = WebConfigurationManager.AppSettings["cn"];

        /// <summary>
        /// 查询数据库(sql语句)
        /// </summary>
        /// <param name="sqlString">sql查询字符串</param>
        /// <returns>返回数据集</returns>
        public static DataSet Select(string sqlString)
        {
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                SqlDataAdapter da = new SqlDataAdapter(sqlString, cn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
        }
发表于 2013-4-23 02:44:51 | 显示全部楼层
静态方法不可能面面俱到,重载也不要过度使用,太多了就不好用了
发表于 2013-4-23 02:49:31 | 显示全部楼层
举一反三,可以看看我写的图片类,只留下3个,是最常用的:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ZooleeBasic
{
    public class ZImage
    {
        public static readonly string[] ImageFileExtensions = { ".bmp", ".gif", ".jpg", ".jpeg", ".png" };
        public static readonly ImageFormat[] ImageFormats = { ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Jpeg, ImageFormat.Jpeg, ImageFormat.Png };
        /// <summary>
        /// 缩放模式
        /// </summary>
        public enum ResizeMod
        {
            定宽定高简单缩放 = 1,
            定宽等比例缩放 = 2,
            定高等比例缩放 = 3,
            定宽定高等比例缩放 = 4
        }
        /// <summary>
        /// 水印位置
        /// </summary>
        public enum WatermarkPosition
        {
            左上=1,
            左中=2,
            左下=3,
            中上=4,
            正中=5,
            中下=6,
            右上=7,
            右中=8,
            右下=9
        }

        /// <summary>
        /// 是否图像文件
        /// </summary>
        /// <param name="filePath">文件的物理路径</param>
        /// <returns></returns>
        public static bool IsImageFile(string filePath)
        {
            string ext = ZFile.GetFileExtension(filePath);
            if(ZString.IsInArray(ext,ImageFileExtensions,false)){return true;}
            return false;
        }

        /// <summary>
        /// 获得一个文件的图像文件格式(非图像文件返回null)
        /// </summary>
        /// <param name="filePath">文件的物理路径</param>
        /// <returns>返回图像文件格式</returns>
        public static ImageFormat GetImageFormat(string filePath)
        {
            string ext = ZFile.GetFileExtension(filePath);
            int id = ZString.GetIDInArray(ext, ImageFileExtensions, false);
            if (id < 0) { return null; }
            return ImageFormats[id];
        }

        /// <summary>
        /// 缩放图像文件
        /// </summary>
        /// <param name="oFilePath">源文件的物理路径</param>
        /// <param name="toFilePath">新文件的物理路径</param>
        /// <param name="toWidth">新的图像宽度</param>
        /// <param name="toHeight">新的图像高度</param>
        /// <param name="resizeMod">缩放模式</param>   
        public static void Resize(string oFilePath, string toFilePath, int toWidth, int toHeight, ResizeMod resizeMod)
        {
            if(!IsImageFile(oFilePath)){ return; }
            Image oImage = Image.FromFile(oFilePath);
            int ow = oImage.Width;
            int oh = oImage.Height;
            int tow = toWidth;
            int toh = toHeight;
            int x = 0;
            int y = 0;
            switch (resizeMod)
            {
                case ResizeMod.定宽定高简单缩放:              
                    break;
                case ResizeMod.定宽等比例缩放:                  
                    toh = oImage.Height * toWidth / oImage.Width;
                    break;
                case ResizeMod.定高等比例缩放:
                    tow = oImage.Width * toHeight / oImage.Height;
                    break;
                case ResizeMod.定宽定高等比例缩放:               
                    if ((double)oImage.Width / (double)oImage.Height > (double)tow / (double)toh)
                    {
                        oh = oImage.Height;
                        ow = oImage.Height * tow / toh;
                        y = 0;
                        x = (oImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = oImage.Width;
                        oh = oImage.Width * toHeight / tow;
                        x = 0;
                        y = (oImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }            
            Image bitmap = new Bitmap(tow, toh);//新建一个bmp图片            
            Graphics g = Graphics.FromImage(bitmap);//新建一个画板            
            g.InterpolationMode = InterpolationMode.High;//设置高质量插值法            
            g.SmoothingMode = SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度            
            g.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充            
            g.DrawImage(oImage, new Rectangle(0, 0, tow, toh), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制原图片的指定部分
            try
            {   
                ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath));
                bitmap.Save(toFilePath, GetImageFormat(oFilePath));//以原图片格式保存缩放图
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                oImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        /// <summary>
        /// 加图片水印
        /// </summary>
        /// <param name="oFilePath">源文件的物理路径</param>
        /// <param name="toFilePath">新文件的物理路径</param>
        /// <param name="watermarkPath">水印图片的物理路径</param>
        /// <param name="watermarkPosition">水印位置</param>
        public static void ImageWatermark(string oFilePath, string toFilePath, string watermarkPath, WatermarkPosition watermarkPosition)
        {
            string ext = ZFile.GetFileExtension(oFilePath);
            if (!ZString.IsInArray(ext, ImageFileExtensions, false)) { return; }
            int x = 0;
            int y = 0;
            Image img = Bitmap.FromFile(oFilePath);
            Image waterimg = Image.FromFile(watermarkPath);
            switch(watermarkPosition)
            {
                case WatermarkPosition.左上:
                    x=10;
                    y=10;
                    break;
                case WatermarkPosition.左中:
                    x = 10;
                    y = img.Height / 2 - waterimg.Height / 2;
                    break;
                case WatermarkPosition.左下:
                    x = 10;
                    y = img.Height - waterimg.Height - 10;
                    break;
                case WatermarkPosition.中上:
                    x = img.Width / 2 - waterimg.Width / 2;
                    y = 10;
                    break;
                case WatermarkPosition.正中:
                    x = img.Width / 2 - waterimg.Width / 2;
                    y = img.Height / 2 - waterimg.Height / 2;
                    break;
                case WatermarkPosition.中下:
                    x = img.Width / 2 - waterimg.Width / 2;
                    y = img.Height - waterimg.Height -10;
                    break;
                case WatermarkPosition.右上:
                    x = img.Width - waterimg.Width - 10;
                    y = 10;
                    break;
                case WatermarkPosition.右中:
                    x = img.Width - waterimg.Width - 10;
                    y = img.Height / 2 - waterimg.Height / 2;
                    break;
                case WatermarkPosition.右下:
                    x = img.Width - waterimg.Width - 10;
                    y = img.Height - waterimg.Height - 10;
                    break;
                default:
                    break;            
            }
            Graphics g = Graphics.FromImage(img);
            g.DrawImage(waterimg, new Rectangle(x, y, waterimg.Width, waterimg.Height));
            try
            {
                ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath));
                img.Save(toFilePath, GetImageFormat(oFilePath));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                waterimg.Dispose();
                img.Dispose();
                g.Dispose();
            }
        }




发表于 2013-4-23 02:50:42 | 显示全部楼层
        /// <summary>
        /// 加文字水印
        /// </summary>
        /// <param name="oFilePath">源文件的物理路径</param>
        /// <param name="toFilePath">新文件的物理路径</param>
        /// <param name="letter">水印文字</param>
        /// <param name="font">水印文字的文本格式</param>
        /// <param name="color">水印文字的颜色</param>
        /// <param name="watermarkPosition">水印位置</param>
        public static void LetterWatermark(string oFilePath, string toFilePath, string letter, Font font, Brush br, WatermarkPosition watermarkPosition)
        {
            string ext = ZFile.GetFileExtension(oFilePath);
            string[] tmp = { ".bmp", ".jpg", ".jpeg" };
            if (!ZString.IsInArray(ext, tmp, false)) { return; }
            float x = 0;
            float y = 0;
            Image img = Bitmap.FromFile(oFilePath);
            float len = ZString.GetStringLength(letter);
            float size = font.GetHeight();
            float letterLength = len * size;
            float letterHeight = size;
            switch (watermarkPosition)
            {
                case WatermarkPosition.左上:
                    x = 10;
                    y = 10;
                    break;
                case WatermarkPosition.左中:
                    x = 10;
                    y = img.Height / 2 - letterHeight / 2;
                    break;
                case WatermarkPosition.左下:
                    x = 10;
                    y = img.Height - letterHeight - 10;
                    break;
                case WatermarkPosition.中上:
                    x = img.Width / 2 - letterLength / 2;
                    y = 10;
                    break;
                case WatermarkPosition.正中:
                    x = img.Width / 2 - letterLength / 2;
                    y = img.Height / 2 - letterHeight / 2;
                    break;
                case WatermarkPosition.中下:
                    x = img.Width / 2 - letterLength / 2;
                    y = img.Height - letterHeight - 10;
                    break;
                case WatermarkPosition.右上:
                    x = img.Width - letterLength - 10;
                    y = 10;
                    break;
                case WatermarkPosition.右中:
                    x = img.Width - letterLength - 10;
                    y = img.Height / 2 - letterHeight / 2;
                    break;
                case WatermarkPosition.右下:
                    x = img.Width - letterLength - 10;
                    y = img.Height - letterHeight - 10;
                    break;
                default:
                    break;
            }
            Graphics g = Graphics.FromImage(img);
            g.DrawString(letter, font, br, x, y);
            try
            {
                ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath));
                img.Save(toFilePath, GetImageFormat(oFilePath));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                img.Dispose();
                g.Dispose();
                br.Dispose();
                font.Dispose();
            }
        }



    }
}
 楼主| 发表于 2013-4-23 06:31:48 | 显示全部楼层
apollosun123 发表于 2013-4-23 02:50
///
        /// 加文字水印
        ///

我的类库也有,可以去比较下,哈哈
发表于 2013-4-23 09:06:23 | 显示全部楼层
站长苏飞 发表于 2013-4-23 06:31
我的类库也有,可以去比较下,哈哈

你的源码我下载了,应该是目前开源的基础类中最全的了

不过最好是做一下重构,去掉一些不常用的方法,要讲究可用性。

尤其要关注一下方法命名,这个很重要。
 楼主| 发表于 2013-4-23 09:08:53 | 显示全部楼层
apollosun123 发表于 2013-4-23 09:06
你的源码我下载了,应该是目前开源的基础类中最全的了

不过最好是做一下重构,去掉一些不常用的方法, ...

比如说那个命名不合理,或者是说那个方法你感觉应该去掉的,可以提提建议
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-27 08:04

© 2014-2021

快速回复 返回顶部 返回列表