码迷,mamicode.com
首页 > 数据库 > 详细

winform的DBhelper

时间:2016-11-08 02:12:53      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:nbsp   reading   nstat   ext   client   orm   int   new   using   

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

标签:nbsp   reading   nstat   ext   client   orm   int   new   using   

原文地址:http://www.cnblogs.com/a1111/p/6041098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!