博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2011-08-17 09:17  ☆Keep★Moving☆  阅读(291)  评论(0编辑  收藏  举报

在過去開發過程中,我一直對數據庫的操作需要寫重復的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);

 

            }