using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Configuration;
using System.Collections;
using System.Data;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Controls;



namespace Dell{
    public class Ops
    {
        public static string dbPath = "Data Source =" + Environment.CurrentDirectory + "/test.db";
        private SQLiteCommand command = new SQLiteCommand();
        private SQLiteConnection connection = null;

        public Ops() { }
       //重载构造函数
        public  Ops(string dbPath)
        {
            initCon(dbPath);
            Boolean b = judgeConn();
            if( b == false)
            {
                MessageBox.Show("数据库开启失败!");
            }
        }

        //建立connection ,并返回链接     
        public static SQLiteConnection buildConn()
        {
            SQLiteConnection conn = new SQLiteConnection(dbPath);//创建数据库实例,指定文件位置
            conn.Open();//打开数据库,若文件不存在会自动创建
            return conn;
        }
        //建立connection,不打开
        public  void initCon(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
        }
        //判断连接状态
        private Boolean judgeConn()
        {
            Boolean isOpen = true;
            if (connection.State != ConnectionState.Open)
            {
                try
                {
                    //initCon(dbPath);
                    connection.Open();

                }catch(Exception){
                    isOpen = false;
                }
            }
            return isOpen;            
        }
     
     
    }

}

 

posted on 2015-03-06 11:40  岳阳楼  阅读(164)  评论(0编辑  收藏  举报