MySQL q4m queue operation encapsulation (2)

Source: Internet
Author: User

/// <Summary>
/// Q4m queue operation base class
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// Author: luyifeng
/// Createday: 2013 05 02
Public abstract class myq4m <t>: iq4m <t>
{
Private bool _ isgetdata = false;

Private mysqlconnection _ conn = NULL;

Private readonly string _ connectionkey = NULL;

Public t queueitem {Get; set ;}

Protected ganjiq4m (string connectionkey)
{
_ Connectionkey = connectionkey;
}

Public t first ()
{
Dispose ();

Getdb ();

Queueitem = getitem ();

If (queueitem! = NULL)
{
_ Isgetdata = true;
}

Return queueitem;
}

Protected abstract string getitemsql ();

Private t getitem ()
{
String strsql = getitemsql ();

If (string. isnullorempty (strsql ))
{
Throw new exception ("No SQL statement is set to get the queue ");
}

VaR cmd = new mysqlcommand (strsql, _ conn );

Using (idatareader reader = cmd. executereader ())
{
If (reader. Read ())
{
VaR que = entityhelper. transformpoco <t> (Reader );

Return que;
}
}

Return default (t );
}

Public void remove ()
{
// The first data goes out of the queue
Const string strsql = "select queue_end ();";

Excutesql (strsql );

// Restore the status
Clearstate ();
}

Public void rollback ()
{
// Roll back data
Const string strsql = "select queue_abort ();";

Excutesql (strsql );

// Restore the status
Clearstate ();
}

Private void excutesql (string SQL)
{
VaR cmd = new mysqlcommand (SQL, _ conn );
Cmd. executenonquery ();
}

Private void getdb ()
{
If (string. isnullorempty (_ connectionkey ))
{
Throw new exception ("no connection string key configured ");
}

VaR connectionstring = system. configuration. configurationmanager. connectionstrings [_ connectionkey]. connectionstring;

_ Conn = new mysqlconnection (connectionstring );

_ Conn. open ();
}

Private void clearstate ()
{
_ Isgetdata = false;
Queueitem = default (t );

If (_ Conn! = NULL)
{
If (_ conn. State = connectionstate. open)
{
_ Conn. Close ();
}

_ Conn. Dispose ();
}
}

Public void dispose ()
{
If (_ isgetdata)
{
Rollback ();
}
}

}

Related Article

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.