[Original, Share] DBHelper continued

Source: Internet
Author: User

have been thinking how to make dbhelper simpler, more flexible, more rigid. Finally I released the first open source version of DBHelper. This helper will use System.Data.DbHelper as the namespace. The internal drive and content session are used to manage. As a user, you only need to do two things. 1, register a driver; 2. Inherit the Dal to create your own data processing layer. DBHelper requires lock to open a transaction for transactional operations; Unlock commits the transaction. Automatic transaction rollback When an error occurs (except for the DAL that received the transaction). Multiple Dal can unify transactions through the group () method (even if the drivers are different). If there are multiple nested relationships, and each dal has lock, then the transaction is the outermost.

The invocation method is simple

1. Registered Driver

            DbService.Instance.RegistDriver ("sql"typeof  (SqlHelper));            DbService.Instance.RegistDriver ("oracle"typeof(Msoraclehelper));

2. General Enquiry

         PublicDataTable GetSource (DateTime time) {varsql =NewStringBuilder (). Append ("Select Checktime,sn_name,badgenumber")                . Append ("From checkinout c")                . Append ("Left join UserInfo u")                . Append ("On C.userid = U.userid")                . AppendFormat ("WHERE CONVERT (VARCHAR (), c.checktime,23) = ' {0} '", time. ToString ("YYYY-MM-DD")); CommandText=SQL.            ToString (); returngetdatatable (); }

3. Parametric submission

         Public BOOLsynccardpress (DataTable source,datetime time) {if(Source = =NULL|| Source. Rows.Count = =0)return true;            Lock (); varD = time. ToString ("YYYY-MM-DD"); CommandText=string. Format ("Delete from Tb_roll_record R WHERE to_char (r.presstime, ' yyyy-mm-dd ') = ' {0} '", D);            ExecuteNonQuery (); CommandText="INSERT into Tb_roll_record VALUES (: Userid,:machine,to_date (:p resstime, ' yyyy-mm-dd hh24:mi:ss '), sysdate)"; varUserId = addparameter<oracleparameter> ("userid"); varMachine = addparameter<oracleparameter> (" Machine"); varPresstime = addparameter<oracleparameter> ("presstime"); foreach(DataRow rowinchsource. Rows) {userid.setvalue (row. to<string> ("Badgenumber"). TrimStart ('0'). PadLeft (4,'0')); Machine. SetValue (row. to<string> ("Sn_name")); Presstime. SetValue (row. to<DateTime> ("Checktime").                Todatetimestring ());                ExecuteNonQuery (); if(!Success)                    {Debug.WriteLine (error.message);  Break;            }} UnLock (); returnSuccess; }

4. Using Stored Procedures

         Public voidCalc (DateTime time) {CommandText="Calcattendancebyday"; CommandType=CommandType.StoredProcedure; Addparameter<OracleParameter> ("XType"). SetValue (1); Addparameter<OracleParameter> ("XIDs"). SetValue (""); Addparameter<OracleParameter> ("Xday"). SetValue (time. AddDays (-1)); Addparameter<OracleParameter> ("XCMT"). SetValue (1);            Lock ();            ExecuteNonQuery ();        UnLock (); }

5. Description

The release version has removed the direct assignment method to commantext and changed to prepare (...). To carry out

6, where there is download click here

Well, I wish you all a pleasant use.

[Original, Share] DBHelper continued

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.