winform的DBhelper
Posted 穆雄雄的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform的DBhelper相关的知识,希望对你有一定的参考价值。
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();
}
}
}
}
以上是关于winform的DBhelper的主要内容,如果未能解决你的问题,请参考以下文章