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

c#操作SQLIte

时间:2015-03-06 12:40:27      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

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;            
        }
        //抽象改变表数据的命令执行函数
        public int ChangeCommand(string commandText, SQLiteParameter[] commandParameters)
        {
            int changedNo = 0;
            judgeConn();
            command.Connection = connection;
            command.CommandText = commandText;
            if (commandParameters != null)
            {
                command.Parameters.AddRange(commandParameters);
                changedNo = command.ExecuteNonQuery();

            }
            else
            {
                changedNo = command.ExecuteNonQuery();
            }
            return changedNo;
        }
        //抽象出不改变表数据的函数
        public SQLiteDataReader noChangeCommand(string commandText)
        {
            
            judgeConn();
            //command.Connection = connection;
            //command.CommandText = commandText;
            SQLiteCommand command = new SQLiteCommand(commandText, connection);
            SQLiteDataReader sdr = command.ExecuteReader();   
            return sdr;
        }
     
    }

}

 

c#操作SQLIte

标签:

原文地址:http://www.cnblogs.com/yueyanglou/p/4317779.html

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