using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace TicketSystem
{
public class DBHelper
{
string conStr = "Data Source=.;Initial Catalog=Ticket;Integrated Security=True";
private SqlConnection _connection;
/// <summary>
/// 公共属性,获取连接对象
/// </summary>
public SqlConnection Connection {
get{
if(_connection==null){
_connection=new SqlConnection(conStr);
}
return _connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnect() {
if (_connection.State == ConnectionState.Closed)//关闭状态
{
_connection.Open();
}
else if (_connection.State == ConnectionState.Broken)//断开状态
{
_connection.Close();
_connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseConnect() {
if (_connection.State == ConnectionState.Open
|| _connection.State == ConnectionState.Broken)
{
_connection.Close();
}
}
}
}