A Database Class C # With the KeepConnection Switch

Source: Internet
Author: User

Using System;
Using System. Data;
Using System. Data. SqlClient;

Namespace cnkk. Data
{
/// <Summary>
/// SQL Database.
/// </Summary>
Public class Database: DisposableBase
{
Private string connectionString;
Private SqlConnection connection;

Protected Database (string connectionString)
{
This. connectionString = connectionString;
}

Protected Database (SqlConnection connection)
{
This. connection = connection;
}

Protected override void DisposeManaged ()
{
Base. DisposeManaged ();

This. connection. Dispose ();
}

Private SqlConnection GetConnection ()
{
If (KeepConnection)
{
Return this. connection;
}
Else
{
Return new SqlConnection (ConnectionString );
}
}

Protected SqlConnection Connection
{
Get {return this. connection ;}
}

Private int commandTimeout = 30;

/// <Summary>
/// Set the waiting time (in seconds ).
/// </Summary>
Public static int CommandTimeout
{
Set
{
This. commandTimeout = value;
}
}

/// <Summary>
/// Database connection string.
/// </Summary>
Public string ConnectionString
{
Get {return connection. ConnectionString ;}
}


Private bool keepConnection = false;

/// <Summary>
/// Maintain the database connection. Setting true to improve the database access performance is not guaranteed to be thread-safe.
/// </Summary>
Public virtual bool KeepConnection
{
Get
{
Return keepConnection;
}

Set
{
If (keepConnection! = Value)
{
KeepConnection = value;

If (keepConnection = true)
{
If (connection. State = ConnectionState. Closed)
Connection. Open ();
}
Else
{
Connection. Close ();
}
}
}
}
}
}


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.