• 对数据库表操作,统一的方法。


    using System;
    using System.Collections.Generic;
    using System.Text;
    using SW.Henu.DAO;
    using System.Data;
    
    namespace LogisManageLibrary
    {
        /// <summary>
        /// 所有操作表类的抽象基类
        /// </summary>
        public abstract class AManage
        {
            /// <summary>
            /// 对应表的字段名称列表
            /// </summary>
            public List<string> attributeList { set; get; }
    
            /// <summary>
            /// 对应表的字段名称列表
            /// </summary>
            public List<string> valueList { set; get; }
    
            /// <summary>
            /// 对应的表名
            /// </summary>
            public string tableName { set; get; }
    
            // 声明委托
            public delegate StringBuilder SetSQLBySelf();
            /// <summary>
            /// 初始化字段列表
            /// </summary>
            public AManage()
            {
                attributeList = new List<string>();
                valueList = new List<string>();
            }
    
            /// <summary>
            /// 初始化表和字段的信息,绑定数据表的定义结构
            /// </summary>
            /// <example>tableName="Table";attributeList.Add("Field");</example>
            public abstract void Initialize();
    
            /// <summary>
            /// 求解查询语句
            /// </summary>
            /// <returns>返回查询表</returns>
            /// <example>string ps_Sql = "select Guid,SimpName as  客户简称,[Name] as 客户名称,LinkMan as 联系人,Telephone as 电话,Fax as 传真,Address as 地址,Zip as 邮编,SellType as 销售类别,ProductName as 产品名称,Remark as  备注,Case when IsEnable=1 then '停用' when IsEnable=0 then '可用' end as 是否可用  from Client ";</example>
            public abstract StringBuilder SetSelectSQL();
    
            /// <summary>
            /// 求解插入的SQL语句
            /// </summary>
            /// <returns></returns>
            /// <example>strSql.Append("insert into [Client](");</example>
            public abstract StringBuilder SetInsertSQL();
    
            /// <summary>
            /// 求解更新的SQL语句
            /// </summary>
            /// <returns></returns>
            /// <example>strSql.Append("update Client set ");</example>
            public abstract StringBuilder SetUpdateSQL();
    
            /// <summary>
            /// 求解删除的SQL语句
            /// </summary>
            /// <returns></returns>
            /// <example>string ps_Sql = "delete  from Client  where  Guid='" + ClientGuid + "'";</example>
            public abstract StringBuilder SetDeleteSQL();
    
            /// <summary>
            /// 通过pSql得到当前是新增状态还是修改状态
            /// </summary>
            /// <param name="pObj">信息集实例</param>
            /// <param name="pSql">string pSql = "";pSql = "SELECT Guid   FROM   Client  " +"where Guid  ='" + pObj.Guid + "'";</param>
            /// <returns>返回True或False</returns>
            private bool SaveStatus(string pSql)
            {
                CommonInterface pComm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    DataTable pDT = pComm.ExeForDtl(pSql);
                    pComm.Close();
                    if (pDT.Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e)
                {
                    pComm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 保存数据
            /// </summary>
            /// <param name="pObj">信息集实体类</param>
            /// <param name="pSql">string pSql = "";pSql = "SELECT Guid   FROM   Client  " +"where Guid  ='" + pObj.Guid + "'";</param>
            /// <returns>返回保存成功(true)或失败(false)</returns>
            public bool Save(string pSql)
            {
                try
                {
                    if (SaveStatus(pSql) == false)
                    {
                        return Insert();
                    }
                    else
                    {
    
                        return Update();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
    
            /// <summary>
            /// 得到数据
            /// </summary>
            /// <returns></returns>
            public DataTable Select()
            {
                CommonInterface pObj_Comm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    DataTable pDTMain = pObj_Comm.ExeForDtl(SetSelectSQL().ToString());
                    pObj_Comm.Close();
                    return pDTMain;
                }
                catch (Exception e)
                {
                    pObj_Comm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 增加一条数据
            /// </summary>
            public bool Insert()
            {
                CommonInterface pComm = CommonFactory.CreateInstance(CommonData.sql);
    
                try
                {
                    pComm.Execute(SetInsertSQL().ToString());//执行Sql语句无返回值
                    pComm.Close();
                    return true;
                }
                catch (System.Exception e)
                {
                    pComm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 更新一条数据
            /// </summary>
            public bool Update()
            {
    
                CommonInterface pComm = CommonFactory.CreateInstance(CommonData.sql);
    
                try
                {
                    pComm.Execute(SetUpdateSQL().ToString());//执行Sql语句无返回值
                    pComm.Close();
                    return true;
                }
                catch (System.Exception e)
                {
                    pComm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 删除
            /// </summary>
            /// <returns></returns>
            public void Delete()
            {
                CommonInterface pObj_Comm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    pObj_Comm.Execute(SetDeleteSQL().ToString());
                    pObj_Comm.Close();
                }
                catch (Exception e)
                {
                    pObj_Comm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 由参数的SQL语句增删改数据库
            /// </summary>
            public void NotSelect(string pSql)
            {
                CommonInterface pObj_Comm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    pObj_Comm.Execute(pSql);
                    pObj_Comm.Close();
                }
                catch (Exception e)
                {
                    pObj_Comm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 得到数据
            /// </summary>
            /// <returns></returns>
            public DataTable Select(string pSql)
            {
                CommonInterface pObj_Comm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    DataTable pDTMain = pObj_Comm.ExeForDtl(pSql);
                    pObj_Comm.Close();
                    return pDTMain;
                }
                catch (Exception e)
                {
                    pObj_Comm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 通过委托得到数据
            /// </summary>
            /// <returns></returns>
            public DataTable SelectBySelf(SetSQLBySelf sql)
            {
                CommonInterface pObj_Comm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    DataTable pDTMain = pObj_Comm.ExeForDtl(sql().ToString());
                    pObj_Comm.Close();
                    return pDTMain;
                }
                catch (Exception e)
                {
                    pObj_Comm.Close();
                    throw e;
                }
            }
    
            /// <summary>
            /// 由委托增删改数据库
            /// </summary>
            public void NotSelect(SetSQLBySelf sql)
            {
                CommonInterface pObj_Comm = CommonFactory.CreateInstance(CommonData.sql);
                try
                {
                    pObj_Comm.Execute(sql().ToString());
                    pObj_Comm.Close();
                }
                catch (Exception e)
                {
                    pObj_Comm.Close();
                    throw e;
                }
            }
        }
    }
    

    这写代码定义了所有的增删改查功能,只需要重写方法,或者委托,或者直接调用写入SQL语句即可。
    该代码写了三种查询方式。重写,委托和直接调用。具体怎么用我还没有事件,但是经过老师的测试,很好用。

    和大家分享下。

  • 相关阅读:
    [软件安装]MYSQL
    [软件安装]JDK
    maven仓库错误
    java.util.Optional
    【原创】Elasticsearch无宕机迁移节点
    【原创】MapReduce备份Elasticsearch数据到HDFS(JAVA)
    python3项目打包成exe可执行程序
    python的frame切换
    switch_to_frame() switch_to.frame()
    如何快速查找透明的icon,让你再也不缺icon的方法
  • 原文地址:https://www.cnblogs.com/ruhuaxiao/p/3311389.html
Copyright © 2020-2023  润新知