把数据库连接放到Application里面有何不好?大家进来评评
微软说ADO.NET技术是Disconnected的,建议针对每次数据的操作进行一次数据库的Open和Close,其目的包括(1)减少连接占用的资源(2)减少死锁的发生 等等
我不清楚把数据库连接放到Application[]里面有何不好,在整个应用过程中只有一个连接.是否访问量比较大的话,该连接的负担太重?或者很容易造成死锁?
下面是我写的一段代码示例,大家看看,有何不妥
class SqlBase
{
SqlConnection m_dbConection;
String m_ConnectionString;
public SqlBase()
{
m_ConnectionString = "server=SHPC147;User ID=sa;Password=;database=Duwamish7;Connection Reset=FALSE"
m_dbConnection = new SqlConnection(m_ConnectionString);
m_dbConnection.Open();
}
public DataSet GetDataSet()
{
...
}
public SqlDataReader GetReader(string sql,out SqlDataReader dr)
{
...
}
}
//----in gobal.asa----
Application_OnStart()
{
SqlBase g_dbController = new SqlBase();
Application["dbController"] = g_dbController;
}
//---get Application["dbController"],create new instance if it lost
class Common
{
public static SqlBase GetDbController()
{
SqlBase dbController;
if (null != Application["dbController"])
{
return (SqlBase)Application["dbController"]
}
else
{
SqlBase dbController = new SqlBase();
Application["dbController"] = dbController;
return dbController;
}
}
}
//---anywhere in use---
//-------example----
SqlDataReader dr = null;
sql = "select * from Product"
Common.GetDbController.GetReader(sql,dr);