• 簡化SQL Insert、Update、Delete、Select的方法


    在過去開發過程中,我一直對數據庫的操作需要寫重復的insert update delete select函數感到很討厭,所以我之前寫了一些簡化的方法,現重新整理一下給大家分享

    ,希望對大家有所幫助,代碼如下:

     

    //對數據庫的所有操作只是調用Oper或Select兩個函數就可以了,只是構造不同的class和sql

                      private string insertsql = "insert .....";

                private string updatesql = "update .....";

                private string deletesql = "delete .....";

                private string selectsql = "select .....";

                public class SampleClass   //定義實體類,變量名和sql裡的輸入變量名對應

                {

                    private Int32 _AttKey = -1;

                    private String _EqStyle = "";

                    //。。。。。。。。。。更多類成員

                    public Int32 AttKey

                    {

                        get{ }

                        set{ }

                    }

                    //。。。。。。。。。。。。。。

                }

                private System.Data.SqlClient.SqlParameter[] GetParameters(System.Object obj)   //構造SqlParameter對象函數

                {

                    System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties();

                    System.Data.SqlClient.SqlParameter[] theParameters = new System.Data.SqlClient.SqlParameter[properties.Length];

                    int i = 0;

                    foreach (System.Reflection.PropertyInfo property in properties)

                    {

                        theParameters[i] = new System.Data.SqlClient.SqlParameter("@" + property.Name,Convert.ChangeType(property.GetValue(obj, null),property.GetType()));

                        i = i + 1;

                    }

                    return theParameters;

                }

     

     

                public Song.Model.ExecuteResultEnum Oper(System.Object obj, string sqlstr)  //執行操作函數

                {

                    try

                    {

                        SqlParameter[] theParameters = new SqlParameter[] { new SqlParameter() };

                        theParameters = GetParameters(obj);

                        SQLServerHelper.ExecuteNonQuery(sqlstr, theParameters);

                        return ExecuteResultEnum.Succeed;

                    }

                    catch

                    {

                        return ExecuteResultEnum.Failed;

                    }

                }

               public System.Collections.ArrayList Select<T>(System.Object obj, string sqlstr) //執行select函數

                {

                    try

                    {

                        SqlParameter[] theParameters = new SqlParameter[] { new SqlParameter() };

                        theParameters = GetParameters(obj);

                        System.Collections.ArrayList arr=SQLServerHelper.ExecuteSelect(sqlstr, theParameters);

                        return arr;

                    }

                    catch

                    {

                        return null;

                    }

                }

                private void oop()//調用

                {

                    SampleClass sc = new SampleClass();

                    sc.AttKey = 1;

                    sc.EqStyle = "ww";

                    Song.Model.ExecuteResultEnum eResult = Oper(sc, insertsql);

                    Song.Model.ExecuteResultEnum eResult = Oper(sc, updatesql);

                    Song.Model.ExecuteResultEnum eResult = Oper(sc, deletesql);

                    System.Collections.ArrayList arr = Select<SampleClass>(sc, selectsql);

     

                }

  • 相关阅读:
    Spring AOP日志实现(一)
    ArrayList和LinkedList的区别
    webmagic保存数据
    Java静态代码块、构造代码块执行顺序问题
    为Spring Cloud Ribbon配置请求重试(Camden.SR2+)
    centos关机与重启命令
    解决win环境下访问本机虚拟机中centos7 ftp服务器的问题
    【MongoDB异常】Exception authenticating MongoCredential解决方法
    mongodb解决只能本地连接不能远程连接问题
    Windows Server 2008 R2提示api-ms-win-crt-runtime-l1-1-0.dll 丢失解决方法
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/2142019.html
Copyright © 2020-2023  润新知