首页 > 开发 > 综合 > 正文

一个有KeepConnection开关的C#的Database类

2024-07-21 02:18:11
字体:
来源:转载
供稿:网友
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();
}
}
}
}
}
}



  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表