Data tier uses DBHelper.dll to reduce workload

Source: Internet
Author: User
Tags static class

The steps to develop after the requirements have been determined are generally as follows: building up a data table, building a model, building a data manipulation layer, and finally calling it in the page. About the data manipulation layer, because a lot of operations are focused on additions, updates, deletions and other simple operations, and my previous writing is that each time the model is created to write this way, and to assign values to the field in the model, which cost me a lot of energy, so I want to find a way, Are there any basic methods for adding, updating, deleting, and so on, to automatically identify database fields after the model is established? So I found the DBHelper.dll. At present, this component can be downloaded in Baidu.

First take a look at my previous database operation methods:

 public int Add (Wymodel.            Wy_wapchannel model) {StringBuilder strSQL = new StringBuilder ();            Strsql.append ("INSERT INTO Wy_wapchannel (");            Strsql.append ("Title,wapid,state,orderid)");            Strsql.append ("values");            Strsql.append ("@title, @wapid, @state, @orderid)");            Strsql.append ("; select Scope_identity ();");                        sqlparameter[] Parameters ={new SqlParameter ("@title", sqldbtype.varchar,200),                        New SqlParameter ("@wapid", SqlDbType.Int), New SqlParameter ("@state", SqlDbType.Int),            New SqlParameter ("@orderid", SqlDbType.Int)}; Parameters[0]. Value = model.            Title; PARAMETERS[1]. Value = model.            Wapid; PARAMETERS[2]. Value = model.            State; PARAMETERS[3]. Value = model.            OrderId;            Object obj = Sqlhelper.getsingle (strsql.tostring (), parameters); If(obj = = null) return 0;        else return Convert.ToInt32 (obj); }

The above is my insert statement, where I need the model for data manipulation, to be assigned. And once I add a field to the database or delete a field, these statements and parameters need to be modified, and not only the INSERT statement needs to be modified, updates and other statements may need to be modified, not high flexibility.

I now introduce the DBHelper.dll component and use its own method for database connectivity:

public static class DBF    {public        static readonly dbhelper.dbfactory Dbweiyun;        Static DBF ()        {            Dbweiyun = new Dbhelper.dbfactory ( system.configuration.configurationmanager.connectionstrings["Wyconn"]. ConnectionString);        }    }

Here "Wyconn" is a parameter of database connection in my Webconfig configuration page, through this component's Dbfactory method can facilitate the connection operation of different database.

After this step, I'm going to map the built model to the database:

public class TableMappings    {        static tablemappings ()        {            Initweiyun ();        }        public static dbhelper.tablemapping wy_businessactivity {get; private set;}        public static dbhelper.tablemapping Wy_shopgoods {get; private set;}        public static dbhelper.tablemapping wy_goodscomment {get; private set;}        private static void Initweiyun ()        {            wy_businessactivity = new Dbhelper.tablemapping (Dbf.dbweiyun, "[Wy_ Businessactivity] ");            Wy_shopgoods = new Dbhelper.tablemapping (Dbf.dbweiyun, "[Wy_shopgoods]");            Wy_goodscomment = new Dbhelper.tablemapping (Dbf.dbweiyun, "[Wy_goodscomment]");        }     }

Here "[wy_businessactivity]" and so on are the data tables in the database.

After the mapping is complete, I need to insert the operation only to pass in the parameters, directly invoke the Insert method in the component.

public bool Insertcomment (Wymodel. Wy_goodscomment model)        {           return  wydal. TableMappings.WY_GoodsComment.Insert (model);        }

One thing to note here is that if there is a field in the model that does not appear in the database, add [Modelproperty (True)] to the field to avoid the mapping:

public int Goodsid {set; get;}        public int star {set; get;}        public string Comment {set; get;}        public string OpenID {set; get;}        Public DateTime Intime {set; get;}        <summary>///        0 Product Reviews, 1 event reviews, 2 flea markets, 3 part-time//        </summary> public        int Ctype {set; get;}        <summary>        //non-database avatar        ///</summary> [Modelproperty (true)] public        string Headimgurl {get; set;}        <summary>        //non-database data nickname        ///</summary> [Modelproperty (true)] public        string Nickname {get; set;}

In short, this component can help us simplify the operation of the database without wasting a lot of effort to repeat the operation.

Data tier uses DBHelper.dll to reduce workload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.