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>
/// 設置等待命令執行的時間(以秒為單位)。
/// </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>
/// 保持數據庫連接,設置為 true 可以提供更高數據庫訪問性能,但不保證是線程安全的。
/// </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();
}
}
}
}
}
}