using System;
using System.Data;
using System.Data.SqlClient;

namespace com.hua..li
{
 /// <summary>
 /// 数据库操作
 /// </summary>
 public class pathDB:pathPage
 {

override protected void OnInit(EventArgs e)
  {
   pathInit();
   base.OnInit(e);
  }

protected void pathInit()
  {
   this.ConnectDb();
  }

protected void ConnectDb()
  {
   if(doh == null)
   {
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connString"]);
    doh = new com.path.SqlDbOperHandler(conn);
   }
  }
 }
}
using System;

namespace com.hua.li
 {
  /// <summary>
  /// 表示数据库连接类型。
  /// </summary>
  public enum DatabaseType:byte{SqlServer,OleDb};
  /// <summary>
  /// DbOperHandler 的摘要说明。
  /// </summary>
  public abstract class DbOperHandler
  {
   /// <summary>
   /// 析构函数,释放申请的资源。
   /// </summary>
   ~DbOperHandler()
   {
    conn.Close();
   }
   /// <summary>
   /// 表示数据库连接的类型,目前支持SqlServer和OLEDB
   /// </summary>
   protected DatabaseType dbType=DatabaseType.OleDb;
   /// <summary>
   /// 返回当前使用的数据库连接对象。
   /// </summary>
   /// <returns></returns>
   public System.Data.IDbConnection GetConnection()
   {
    return conn;
   }
   /// <summary>
   /// 条件表达式,用于在数据库操作时筛选记录,通常用于仅需指定表名称和某列名称的操作,如GetValue(),Delete()等,支持查询参数,由AddConditionParameters指定。。
   /// </summary>
   public string ConditionExpress=string.Empty;
   /// <summary>
   /// 当前的SQL语句。
   /// </summary>
   public string SqlCmd=string.Empty;
   /// <summary>
   /// 当前操作所涉及的数据表名称。
   /// </summary>
   protected string tableName=string.Empty;
   /// <summary>
   /// 当前操作所设计的字段名称。
   /// </summary>
   protected string fieldName=string.Empty;
   /// <summary>
   /// 当前所使用的数据库连接。
   /// </summary>
   protected System.Data.IDbConnection conn;
   /// <summary>
   /// 当前所使用的命令对象。
   /// </summary>
   protected System.Data.IDbCommand cmd;
   /// <summary>
   /// 当前所使用的数据库适配器。
   /// </summary>
   protected System.Data.IDbDataAdapter da;

/// <summary>
   /// 用于存储字段/值配对。
   /// </summary>
   protected System.Collections.ArrayList alFieldItems=new System.Collections.ArrayList(10);
   /// <summary>
   /// 用于存储SQL语句中的查询参数。
   /// </summary>
   protected System.Collections.ArrayList alSqlCmdParameters=new System.Collections.ArrayList(5);
   /// <summary>
   /// 用于存储条件表达式中的查询参数。
   /// </summary>
   protected System.Collections.ArrayList alConditionParameters=new System.Collections.ArrayList(5);
   /// <summary>
   /// 重值该对象,使之恢复到构造时的状态。
   /// </summary>
   public void Reset()
   {
    this.alFieldItems.Clear();
    this.alSqlCmdParameters.Clear();
    this.alConditionParameters.Clear();
    this.ConditionExpress=string.Empty;
    this.SqlCmd=string.Empty;
    this.cmd.Parameters.Clear();
    this.cmd.CommandText=string.Empty;
   }
   /// <summary>
   /// 添加一个字段/值对到数组中。
   /// </summary>
   /// <param name="_fieldName">字段名称。</param>
   /// <param name="_fieldValue">字段值。</param>
   public void AddFieldItem(string _fieldName,object _fieldValue)
   {

for(int i=0;i<this.alFieldItems.Count;i++)
    {
     if(((DbKeyItem)this.alFieldItems[i]).fieldName==_fieldName)
     {
      throw new ArgumentException("The field name has existed!");
     }
    }
    this.alFieldItems.Add(new DbKeyItem(_fieldName,_fieldValue));
   }
   /// <summary>
   /// 添加条件表达式中的查询参数到数组中。注意:当数据库连接为SqlServer时,参数名称必须和SQL语句匹配。其它则只需保持添加顺序一致,名称无需匹配。
   /// </summary>
   /// <param name="_conditionName">条件名称。</param>
   /// <param name="_conditionValue">条件值。</param>
   public void AddConditionParameter(string _conditionName,object _conditionValue)
   {
    for(int i=0;i<this.alConditionParameters.Count;i++)
    {
     if(((DbKeyItem)this.alConditionParameters[i]).fieldName==_conditionName)
     {
      throw new ArgumentException("The condition name has existed!");
     }
    }
    this.alConditionParameters.Add(new DbKeyItem(_conditionName,_conditionValue));
   }

/// <summary>
   /// 添加SQL语句中的查询参数到数组中。注意:当数据库连接为SqlServer时,参数名称必须和SQL语句匹配。其它则只需保持添加顺序一致,名称无需匹配。
   /// </summary>
   /// <param name="_paraName">参数名称。</param>
   /// <param name="_paraValue">参数值。</param>
   public void AddSqlCmdParameters(string _paraName,object _paraValue)
   {
    for(int i=0;i<this.alSqlCmdParameters.Count;i++)
    {
     if(((DbKeyItem)this.alSqlCmdParameters[i]).fieldName==_paraName)
     {
      throw new ArgumentException("The sqlcmd parameter name has existed!");
     }
    }
    this.alSqlCmdParameters.Add(new DbKeyItem(_paraName,_paraValue));
   }

public bool Exist(string tableName)
   {
    return this.GetValue(tableName,"count(*)").ToString()!="0";
   }
   /// <summary>
   /// 抽象函数。用于产生Command对象所需的参数。
   /// </summary>
   protected abstract void GenParameters();
   /// <summary>
   /// 根据当前alFieldItem数组中存储的字段/值向指定表中添加一条数据。在该表无触发器的情况下返回添加数据所获得的自动增长id值。
   /// </summary>
   /// <param name="_tableName">要插入数据的表名称。</param>
   /// <returns>返回本数据连接上产生的最后一个自动增长id值。</returns>
   public int Insert(string _tableName)
   {
   
    this.tableName=_tableName;
    this.fieldName=string.Empty;
    this.SqlCmd="insert into "+this.tableName+"(";
    string tempValues=" values(";
    for(int i=0;i<this.alFieldItems.Count-1;i++)
    {
     this.SqlCmd+=((DbKeyItem)alFieldItems[i]).fieldName;
     this.SqlCmd+=",";

tempValues+="@para";
     tempValues+=i.ToString();

tempValues+=",";
    }
    this.SqlCmd+=((DbKeyItem)alFieldItems[alFieldItems.Count-1]).fieldName;
    this.SqlCmd+=") ";

tempValues+="@para";
    tempValues+=(alFieldItems.Count-1).ToString();

tempValues+=")";
    this.SqlCmd+=tempValues;
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    cmd.ExecuteNonQuery();
    cmd.CommandText="select @@identity as id";
    int autoId=Convert.ToInt32(cmd.ExecuteScalar());
    return autoId;
   }

/// <summary>
   /// 根据当前alFieldItem数组中存储的字段/值和条件表达式所指定的条件来更新数据库中的记录,返回所影响的行数。
   /// </summary>
   /// <param name="_tableName">要更新的数据表名称。</param>
   /// <returns>返回此次操作所影响的数据行数。</returns>
   public int Update(string _tableName)
   {
    this.tableName=_tableName;
    this.fieldName=string.Empty;
    this.SqlCmd="update "+this.tableName+" set ";
    for(int i=0;i<this.alFieldItems.Count-1;i++)
    {
     this.SqlCmd+=((DbKeyItem)alFieldItems[i]).fieldName;
     this.SqlCmd+="=";

this.SqlCmd+="@para";
     this.SqlCmd+=i.ToString();

this.SqlCmd+=",";
    }
    this.SqlCmd+=((DbKeyItem)alFieldItems[alFieldItems.Count-1]).fieldName;
    this.SqlCmd+="=";

this.SqlCmd+="@para";
    this.SqlCmd+=(alFieldItems.Count-1).ToString();

if(this.ConditionExpress!=string.Empty)
    {
     this.SqlCmd=this.SqlCmd+" where "+this.ConditionExpress;
    }
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    int effectedLines=this.cmd.ExecuteNonQuery();
    return effectedLines;
   }

/// <summary>
   /// 执行SqlCmd中的SQL语句,参数由AddSqlCmdParameters指定,与ConditionExpress无关。
   /// </summary>
   /// <returns>返回此次操作所影响的数据行数。</returns>
   public int ExecuteSqlNonQuery()
   {
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    return cmd.ExecuteNonQuery();
   }
   /// <summary>
   /// 获取指定表,指定列,指定条件的第一个符合条件的值。
   /// </summary>
   /// <param name="_tableName">表名称。</param>
   /// <param name="_fieldName">字段名称。</param>
   /// <returns>获取的值。如果为空则返回null。</returns>
   public object GetValue(string _tableName,string _fieldName)
   {
    this.tableName=_tableName;
    this.fieldName=_fieldName;
    this.SqlCmd="select "+this.fieldName+" from "+this.tableName;
    if(this.ConditionExpress!=string.Empty)
    {
     this.SqlCmd=this.SqlCmd+" where "+this.ConditionExpress;
    }
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    return cmd.ExecuteScalar();
   }
   /// <summary>
   /// 根据当前指定的SqlCmd获取DataTable。如果ConditionExpress不为空则会将其清空,所以条件表达式需要包含在SqlCmd中。
   /// </summary>
   /// <returns>返回查询结果DataTable。</returns>
   public System.Data.DataTable GetDataTable()
   {
    System.Data.DataSet ds=this.GetDataSet();
    return ds.Tables[0];
   }
   /// <summary>
   /// 根据当前指定的SqlCmd获取DataSet。如果ConditionExpress不为空则会将其清空,所以条件表达式需要包含在SqlCmd中。
   /// </summary>
   /// <returns>返回查询结果DataSet。</returns>
   public System.Data.DataSet GetDataSet()
   {
    this.alConditionParameters.Clear();
    this.ConditionExpress=string.Empty;
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    System.Data.DataSet ds=new System.Data.DataSet();
    this.da.SelectCommand=this.cmd;
    this.da.Fill(ds);
    return ds;
   }
   /// <summary>
   /// 对指定表,指定字段执行加一计数,返回计数后的值。条件由ConditionExpress指定。
   /// </summary>
   /// <param name="_tableName">表名称。</param>
   /// <param name="_fieldName">字段名称。</param>
   /// <returns>返回计数后的值。</returns>
   public int Count(string _tableName,string _fieldName)
   {
    this.tableName=_tableName;
    this.fieldName=_fieldName;
    int count=Convert.ToInt32(this.GetValue(this.tableName,this.fieldName));
    count++;
    this.cmd.Parameters.Clear();
    this.cmd.CommandText=string.Empty;
    this.AddFieldItem(_fieldName,count);
    this.Update(this.tableName);
    return count;
   }

/// <summary>
   /// 对指定表,指定字段执行减一计数,返回计数后的值。条件由ConditionExpress指定。
   /// </summary>
   /// <param name="_tableName">表名称。</param>
   /// <param name="_fieldName">字段名称。</param>
   /// <returns>返回计数后的值。</returns>
   public int Substract(string _tableName,string _fieldName)
   {
    this.tableName=_tableName;
    this.fieldName=_fieldName;
    int count=Convert.ToInt32(this.GetValue(this.tableName,this.fieldName));
    if(count>0)count--;
    this.cmd.Parameters.Clear();
    this.cmd.CommandText=string.Empty;
    this.AddFieldItem(_fieldName,count);
    this.Update(this.tableName);
    return count;
   }

/// <summary>
   /// 根据ConditionExpress指定的条件在指定表中删除记录。返回删除的记录数。
   /// </summary>
   /// <param name="_tableName">指定的表名称。</param>
   /// <returns>返回删除的记录数。</returns>
   public int Delete(string _tableName)
   {
    this.tableName=_tableName;
    this.SqlCmd="delete from "+this.tableName;
    if(this.ConditionExpress!=string.Empty)
    {
     this.SqlCmd=this.SqlCmd+" where "+this.ConditionExpress;
    }
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    return cmd.ExecuteNonQuery();
   }
            /// <summary>
            /// 函数sendMsg需要 __Receive接受者 如果是系统则为 admin 否则为手机号码
            /// </summary>
            /// <param name="_PHONE">手机号码</param>
            /// <param name="_KeyWorld">关键字</param>
            /// <param name="_INFO">信息的基本内容</param>
            /// <param name="_Receive">接受者</param>
            /// <returns></returns>
            public bool SendMsg(string _PHONE, string _KeyWorld, string _INFO, string _Receive)
            {
                bool SendOk;
                if (_PHONE != null || _KeyWorld != null)
                {
                    this.Reset();
                    this.AddFieldItem("PHONE", _PHONE);
                    this.AddFieldItem("KeyWorld", _KeyWorld);
                    this.AddFieldItem("INFO", _INFO);
                    this.AddFieldItem("Receive", _Receive);
                    this.Insert("smsRawRecv").ToString();
                    SendOk = true;
                }
                else
                {
                    SendOk = false;
                    
                }
                return SendOk;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="_PHONE"></param>
            /// <param name="_KeyWorld"></param>
            /// <param name="_INFO"></param>
            /// <param name="_Receive"></param>
            /// <returns></returns>
             //public bool Received(string _PHONE, string _KeyWorld, string _INFO, string _Receive)
             //{
             //    bool Received, SendOk;
             //    if (SendOk)
             //    {
             //        if (_PHONE != null || _KeyWorld != null)
             //        {
             //            this.Reset();
             //            this.AddFieldItem("PHONE", _PHONE);
             //            this.AddFieldItem("KeyWorld", _KeyWorld);
             //            this.AddFieldItem("INFO", _INFO);
             //            this.AddFieldItem("Receive", _Receive);
             //            this.Insert("smsSended").ToString();
             //            Received = true;
             //        }
             //        else
             //        {
             //            Received = false;
             //        }
             //    }
             //    else
             //    {
             //        Received = false;
             //    }
             //}
   /// <summary>
   /// 审核函数。将指定表,指定字段的值进行翻转,如:1->0或0->1。条件由ConditionExpress指定。
   /// </summary>
   /// <param name="_tableName">表名称。</param>
   /// <param name="_fieldName">字段名称。</param>
   /// <returns>返回影响的行数。</returns>
   public int Audit(string _tableName,string _fieldName)
   {
    this.tableName=_tableName;
    this.fieldName=_fieldName;
    this.SqlCmd="update "+this.tableName+" set "+this.fieldName+"=1-"+this.fieldName;
    if(this.ConditionExpress!=string.Empty)
    {
     this.SqlCmd=this.SqlCmd+" where "+this.ConditionExpress;
    }
    this.cmd.CommandText=this.SqlCmd;
    this.GenParameters();
    return cmd.ExecuteNonQuery();
   }

/// <summary>
   /// 释放资源
   /// </summary>
   public void Dispose()
   {
    conn.Close();
   }
  
  }

/// <summary>
  /// 数据表中的字段属性,包括字段名,字段值。
  /// 常用于保存要提交的数据。
  /// </summary>
  public class DbKeyItem
  {
   /// <summary>
   /// 构造函数。
   /// </summary>
   /// <param name="_fieldName">字段名称。</param>
   /// <param name="_fieldValue">字段值。</param>
   public DbKeyItem(string _fieldName,object _fieldValue)
   {
    this.fieldName=_fieldName;
    this.fieldValue=_fieldValue.ToString();
   }
   /// <summary>
   /// 字段名称。
   /// </summary>
   public string fieldName;
   /// <summary>
   /// 字段值。
   /// </summary>
   public string fieldValue;
  }
 }

数据库操作通用函数,增强可重复利用性能C#,asp.net.sql2005的更多相关文章

  1. IT观察】网络通信、图片显示、数据库操作……Android程序员如何利用开源框架

    每个Android 程序员都不是Android应用开发之路上孤军奋战的一个人,GitHub上浩如烟海的开源框架或类库就是前人为我们发明的轮子,有的轮子能提高软件性能,而有的轮子似乎是以牺牲性能为代价换 ...

  2. 如何在高并发环境下设计出无锁的数据库操作(Java版本)

    一个在线2k的游戏,每秒钟并发都吓死人.传统的hibernate直接插库基本上是不可行的.我就一步步推导出一个无锁的数据库操作. 1. 并发中如何无锁. 一个很简单的思路,把并发转化成为单线程.Jav ...

  3. Java数据库操作

    一.JDBC 1.JDBC Java数据库连接,用于Java程序中实现数据库操作功能,java.sql包中提供了执行SQL语句,访问各种数据库的方法,并为各种不同的数据库提供统一的操作接口及类. 2. ...

  4. 使用JdbcTemplate简化JDBC操作 实现数据库操作

    使用Spring JDBC框架方遍简单的完成JDBC操作,满足性能的需求且灵活性高. Spring JDBC框架由4个部分组成,即core.datasource.object.support. org ...

  5. 十三、EnterpriseFrameWork框架核心类库之数据库操作(多数据库事务处理)

    本章介绍框架中封装的数据库操作的一些功能,在实现的过程中费了不少心思,针对不同数据库的操作(SQLServer.Oracle.DB2)这方面还是比较简单的,用工厂模式就能很好解决,反而是在多数据库同时 ...

  6. c# 数据库操作学习

    一. 如何处理数据库连接 1. 数据库连接可以分为“物理连接”和“逻辑连接”(默认使用连接池的情况下Pooling=true): 物理连接:创建数据库连接时,默认会有一定数量的物理连接(默认Min P ...

  7. PHP-Phalcon框架中的数据库操作

    > 本文描述了PHP-Phalcon框架中数据库操作方法,主要讨论Phalcon框架的Model组件中的操作方法.更详细的Model介绍请参考:官方文档 1. 连接数据库 在Phalcon框架中 ...

  8. android中的数据库操作(转)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

  9. asp.net数据库操作类(一)

    Hi Boy, 我现在需要使用asp.net操作access数据库,你来做个.boy听后就开始百度了,最后找到了一个比较好的方法.如下:  C# Code  1234567   <appSett ...

随机推荐

  1. (C/C++学习)12.获取系统时间制作时钟(system()略解)

    说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年.月.日.小时.分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示. 一.对相关函数的学习 1.time_t t ...

  2. HDU - 5894 Pocky(概率)

    HDU5894—Pocky Problem Description: Let’s talking about something of eating a pocky. Here is a Decore ...

  3. HTML学习笔记之标签基础

    目录 1.基本标签 2.链接 3.图像 4.表格 5.列表 6.块与布局 1.基本标签 (1)标题与段落 标签 <h1> ~ <h6> 分别用于定义一至六级标题,标签 < ...

  4. PAT 1117 Eddington Number

    British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, h ...

  5. 6.3.3 使用 shelve 模块操作二进制文件

    Python标准库shelve也提供了二进制文件操作的功能,可以像字典赋值一样来写入二进制文件,也可以像字典一样读取二进制文件,有点类似于NoSQL数据库MongoDB. import shelve ...

  6. 2、ceph-deploy之配置使用RBD

    说明 部署完ceph集群(active+clean状态)之后, 我们来实践下ceph block device(即RBD或RADOS block device). 我们需要在一台新的client节点上 ...

  7. Android第三方开源图片裁剪截取:cropper

     Android第三方开源图片裁剪截取:cropper 很多app都需要裁剪截取图片作为头像.logo之类,而cropper是github上的一个针对Android平台的.第三方开源图片裁剪截取项 ...

  8. 清北学堂模拟赛d1t6 或和异或(xor)

    题目描述 LYK最近在研究位运算,它研究的主要有两个:or和xor.(C语言中对于|和^) 为了更好的了解这两个运算符,LYK找来了一个2^n长度的数组.它第一次先对所有相邻两个数执行or操作,得到一 ...

  9. cogs 167. [USACO Mar07] 月度花费

    167. [USACO Mar07] 月度花费 ★★   输入文件:expense.in   输出文件:expense.out   简单对比时间限制:1 s   内存限制:128 MB Farmer ...

  10. GPS-Graph Processing System 改动源代码经验总结 (四)

    HamaWhite原创,转载请注明出处.欢迎大家增加Giraph 技术交流群: 228591158 本文目的:在改动GPS源代码后,具体描写叙述怎样编译和分发到各Worker节点上. 以下以Graph ...