【发布时间】:2014-06-11 17:24:01
【问题描述】:
我正在为一个 .NET 项目建立一个连接工厂,我想问一下最好的方法是什么。 我有一个问题,以前的 Log 类由于 table LOCKS(或者他们说)而无法正确写入日志,所以我的任务是设置一个新的数据层(希望)解决这个问题以及其他一些小问题问题。
现在,代码:
public sealed class ConnectionFactory
{
//Will be SQL only
private static SqlConnection sqlConnection = null;
private static string connectionString = ConfigurationManager.ConnectionStrings["Development"].ConnectionString;
public static SqlConnection GetConnection()
{
if(sqlConnection == null)
{
sqlConnection = new SqlConnection();
sqlConnection.Open();
}
return sqlConnection;
}
}
我将主要使用程序,但可能有一个或另一个奇怪的请求,如果需要我们会输入一个查询,所以我想我必须以某种方式添加一个 SqlCommand:
private static SqlCommand sqlCommand = null;
public static SqlCommand GetCommand()
{
//Check if sqlConnection is not null (omitted)
if(sqlCommand == null)
{
sqlCommand = sqlConnection.CreateCommand();
}
return sqlCommand;
}
但是,这个命令应该是静态的吗?还是应该在每次执行新查询时创建一个新查询? 主要考虑避免锁,是有多个命令还是只有多个连接引起的?我该如何避免和妥善处理?
【问题讨论】:
标签: c# sql .net table-lock