1个回答
2013-07-11
展开全部
你可以看看这段代码,相信应该有用。在连接之前先判断,如果是打开的则将连接关闭后再重新打开,如果不是,则直接打开
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = @"";//数据库连接语句
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = @"";//数据库连接语句
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询