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

系统 1928 0
      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语句即可。
该代码写了三种查询方式。重写,委托和直接调用。具体怎么用我还没有事件,但是经过老师的测试,很好用。

和大家分享下。

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论