DBhelper

Jun 07, 2016 pm 05:42 PM

1 using System; 2 using System.Configuration; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.Common; 6 using System. Text ; 7 8 public static class Db 9 { ; 11 public static DbProviderFactory Factory = DbProvi

1 using System; 2 using System.Configuration; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.Common; 6 using System.Text; 7   8 public static class Db 9 { ; 11     public static DbProviderFactory Factory = DbProviderFactories.GetFactory(ConnectionString.ProviderName); 12   13     public static DbConnection CreateConnection() 14     { 15         DbConnection con = Factory.CreateConnection(); 16         con.ConnectionString = ConnectionString.ConnectionString; 17         return con; 18     } 19   20     #region 参数 21   22     public static DbParameter CreateParameter(DbParameter param) 23     { 24         return CreateParameter(param.ParameterName, param.Value, param.DbType, param.Size, param.Direction, param.SourceColumn, param.SourceColumnNullMapping, param.SourceVersion); 25     } 26   , ParameterDirection? Direction , bool? SourceColumnNullMapping ) 28     { 29         DbParameter param = Factory.CreateParameter(); 30   31         param.ParameterName = ParameterName; 32         param.Value = Value; 33   34         if (DbType != null) 35             param.DbType = DbType.Value; 36         if (Size != null) 37             param.Size = Size.Value; 38         if (Direction != null) 39             param.Direction = Direction.Value; 40         if (SourceColumn != null) 41             param.SourceColumn = SourceColumn; 42         if (SourceColumnNullMapping != null) 43             param.SourceColumnNullMapping = SourceColumnNullMapping.Value; 44         if (SourceVersion != null) 45             param.SourceVersion = SourceVersion.Value; 46   47         return param; 48     } 49   50     private static DbParameter[] ConvertParameters(object[] parameters) 51     { 52         ListDbParameter> paramList = new ListDbParameter>(); 53   54         for (int i = 0; i parameters.Length; i++) 55         { DbParameterCollection) DbParameterCollection) paramList.Add(CreateParameter(item)); DbParameter) DbParameter); )); 62         } 63   64         return paramList.ToArray(); 65     } 66   67     #endregion 68   69     public static Query Query(string query, params object[] parameters) 70     { 71         return new Query(query, ConvertParameters(parameters)); 72     } 73   74     public static bool Insert(string table, object model) 75     { 76         StringBuilder fields = new StringBuilder(); 77         StringBuilder values = new StringBuilder(); 78         ListDbParameter> paramList = new ListDbParameter>(); 79   80         foreach (var item in model.GetType().GetProperties()) 81         { ,", item.Name); 83             values.AppendFormat("@{0},", item.Name); 84             paramList.Add(CreateParameter("@" + item.Name, item.GetValue(model, null))); 85         } 86   ({), )), paramList.ToArray()).Execute() > 0; 88     } 89   90     public static bool Update(string table, object model, string where, params object[] parameters) 91     { 92         StringBuilder fieldsAndValues = new StringBuilder(); 93         ListDbParameter> paramList = new ListDbParameter>(); 94   95         foreach (var item in model.GetType().GetProperties()) 96         { @{0},", item.Name); 98             paramList.Add(CreateParameter("@" + item.Name, item.GetValue(model, null))); 99         } 100   101         paramList.AddRange(ConvertParameters(parameters)); 102   {) ; 104     } 105 } 106   107 public class Query 108 { 109     #region 构造方法 110   111     public Query(string query, DbParameter[] parameters) 112     { 113         SqlQuery = query; 114         Parameters = parameters; 115     } 116   117     public Query(string query, DbParameter[] parameters, bool isException) 118         : this(query, parameters) 119     { 120         IsException = isException; 121     } 122   123     #endregion 124   125     #region 属性/字段 126   127     private bool IsException { get; set; } 128     public string SqlQuery { get; set; } 129     public DbParameter[] Parameters { get; set; } 130   131     #endregion 132   133     #region 执行基础 134   135     private T ExecuteCommonT>(FuncDbCommand, T> function) 136     { 137         using (DbConnection con = Db.CreateConnection()) 138         using (DbCommand cmd = con.CreateCommand()) 139         { 140             cmd.CommandText = SqlQuery; 141             cmd.Parameters.AddRange(Parameters); 142             con.Open(); 143             T result = function(cmd); 144             cmd.Parameters.Clear(); 145             return result; 146         } 147     } 148   , T exValue = default(T)) 150     { 151         if (IsException) 152             return ExecuteCommonT>(function); 153   154         try 155         { 156             return ExecuteCommonT>(function); 157         } 158         catch (Exception e) 159         { 160             Console.WriteLine(e.ToString()); 161             return exValue; 162         } 163     } 164   165     public void Execute(ActionDbCommand> action) 166     { 167         Execute(cmd => { action(cmd); return 0; }); 168     } 169   170     #endregion 171   172     #region 执行查询 173   () 175     { 176         return Execute(cmd => cmd.ExecuteNonQuery()); 177     } 178   179     public object Scalar() 180     { 181         return Execute(cmd => cmd.ExecuteScalar()); 182     } 183   184     public T ScalarT>() 185     { 186         return Execute(cmd => (T)cmd.ExecuteScalar()); 187     } 188   189     public Query Top(int count) 190     { ({1}) as t0", count, SqlQuery), Parameters); 192     } 193   194     public Single ToSingle() 195     {         { 198             Single s = new Single(); 199   200             using (var dr = cmd.ExecuteReader()) 201             { 202                 if (dr.Read()) 203                 { 204                     string name = string.Empty; 205   206                     for (int i = 0; i dr.FieldCount; i++) 207                     { 208                         name = dr.GetName(i); dr; 210                     } 211                 } 212                 else 213                 { 214                     throw new Exception("Not Find !!"); 215                 } 216             } 217   218             return s; 219         }); 220   221     } 222   223     public DataTable ToDataTable() 224     {         { 227             DbDataAdapter da = Db.Factory.CreateDataAdapter(); 228             da.SelectCommand = cmd; 229             DataTable dt = new DataTable(); 230             da.Fill(dt); 231             return dt; 232         }); 233     } 234   235     public ListT> ToListT>() 236     {         { 239             ListT> list = new ListT>(); 240   241             using (var dr = cmd.ExecuteReader()) 242             { 243                 while (dr.Read()) 244                 { 245                     Type t = typeof(T); 246                     T s = default(T); 247                     string name = string.Empty; 248   249                     for (int i = 0; i dr.FieldCount; i++) 250                     { 251                         name = dr.GetName(i); 252                         var pro = t.GetProperty(name); 253   254                         if (pro != null) , null); 256                     } 257   258                     list.Add(s); 259                 } 260             } 261   262             return list; 263         }, new ListT>()); 264     } 265   266     public override string ToString() 267     { 268         return Scalarstring>(); 269     } 270   271     #endregion 272   273     #region 分页 274   275     private Query RecordCountQuery 276     { 277         get { return Db.Query(string.Format("select count(*) from ({0}) as t0", SqlQuery), Parameters); } 278     } 279   280     private Query PagerResultQuery(string primaryKey, int pageIndex, int pageSize) 281     { ({? " {2} t1.{3} from ({0}) as t1)" : ""), 284             SqlQuery, pageSize, pageIndex * pageSize, primaryKey), Parameters); 285     } 286   recordCount) 288     { ()); 290         return PagerResultQuery(primaryKey, pageIndex, pageSize).ToDataTable(); 291     } 292   recordCount) 294     { 295         return ToPager("Id", pageIndex, pageSize, recordCount); 296     } 297   recordCount) 299     { ()); 301         return PagerResultQuery(primaryKey, pageIndex, pageSize).ToListT>(); 302     } 303   recordCount) 305     { 306         return ToPagerT>("Id", pageIndex, pageSize, recordCount); 307     } 308   309     #endregion 310 } 311   {     { ; } 317         set { Add(name.ToLower(), value); } 318     } 319 } ,网站空间,美国服务器,虚拟主机

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

减少在Docker中使用MySQL内存的使用 减少在Docker中使用MySQL内存的使用 Mar 04, 2025 pm 03:52 PM

本文探讨了Docker中的优化MySQL内存使用量。 它讨论了监视技术(Docker统计,性能架构,外部工具)和配置策略。 其中包括Docker内存限制,交换和cgroups

mysql无法打开共享库怎么解决 mysql无法打开共享库怎么解决 Mar 04, 2025 pm 04:01 PM

本文介绍了MySQL的“无法打开共享库”错误。 该问题源于MySQL无法找到必要的共享库(.SO/.DLL文件)。解决方案涉及通过系统软件包M验证库安装

如何使用Alter Table语句在MySQL中更改表? 如何使用Alter Table语句在MySQL中更改表? Mar 19, 2025 pm 03:51 PM

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

在 Linux 中运行 MySQl(有/没有带有 phpmyadmin 的 podman 容器) 在 Linux 中运行 MySQl(有/没有带有 phpmyadmin 的 podman 容器) Mar 04, 2025 pm 03:54 PM

本文比较使用/不使用PhpMyAdmin的Podman容器直接在Linux上安装MySQL。 它详细介绍了每种方法的安装步骤,强调了Podman在孤立,可移植性和可重复性方面的优势,还

什么是 SQLite?全面概述 什么是 SQLite?全面概述 Mar 04, 2025 pm 03:55 PM

本文提供了SQLite的全面概述,SQLite是一个独立的,无服务器的关系数据库。 它详细介绍了SQLite的优势(简单,可移植性,易用性)和缺点(并发限制,可伸缩性挑战)。 c

在MacOS上运行多个MySQL版本:逐步指南 在MacOS上运行多个MySQL版本:逐步指南 Mar 04, 2025 pm 03:49 PM

本指南展示了使用自制在MacOS上安装和管理多个MySQL版本。 它强调使用自制装置隔离安装,以防止冲突。 本文详细详细介绍了安装,起始/停止服务和最佳PRA

如何为MySQL连接配置SSL/TLS加密? 如何为MySQL连接配置SSL/TLS加密? Mar 18, 2025 pm 12:01 PM

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么? 哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么? Mar 21, 2025 pm 06:28 PM

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

See all articles