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 ();
}
}
}
}
}
}