• (转)c# 操作sqlite 数据库的类


    原文地址:http://www.cnblogs.com/Leo_wl/archive/2010/06/23/1763735.html

    using System;
    using System.Text;
    using System.Data;
    using System.Data.SQLite;
    
    namespace WQ.SQLite.DataBase
    {
    
     
        public class SQLiteDataBase : IDisposable
        {
            SQLiteConnection conn;
            private int iTimeOut = 30;
            private string strErro;
            private string connStr;
    
            public SQLiteDataBase(string _connString)
            {
                connStr = _connString;
            }
    
            private SQLiteCommand CreateCommand(string sqlString)
            {
                this.Open(connString);
                SQLiteCommand command = new SQLiteCommand(sqlString, this.conn);
                command.CommandTimeout = this.TimeOut;
                command.CommandType = CommandType.Text;
                command.CommandText = sqlString;
                return command;
            }
    
            public bool ExcQuery(string sqlString)
            {
                try
                {
                    SQLiteCommand command = new SQLiteCommand(sqlString, new SQLiteConnection(connString));
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                    return true;
                }
                catch (Exception exception)
                {
                    this.strErro = exception.ToString();
                    return false;
                }
            }
    
            public DataTable GetDataTable(string sqlString)
            {
                try
                {
                    SQLiteConnection selectConnection = new SQLiteConnection(connString);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlString, selectConnection);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "myTable");
                    return dataSet.Tables["myTable"];
                }
                catch (Exception exception)
                {
                    this.strErro = sqlString + "\n" + exception.Message;
                    return null;
                }
            }
    
            public bool GetDataTable(string sqlString, ref DataTable DataTable)
            {
                try
                {
                    SQLiteConnection selectConnection = new SQLiteConnection(connString);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlString, selectConnection);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "myTable");
                    DataTable = dataSet.Tables["myTable"];
                    return true;
                }
                catch (Exception exception)
                {
                    this.strErro = sqlString + "\n" + exception.Message;
                    return false;
                }
            }
    
            public bool GetDataTable(string sqlString, SQLiteParameter[] pa, ref DataTable dt)
            {
                try
                {
                    SQLiteConnection selectConnection = new SQLiteConnection(connString);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlString, selectConnection);
                    DataSet dataSet = new DataSet();
                    for (int i = 0; i < pa.Length; i++)
                    {
                        adapter.SelectCommand.Parameters.Add(pa[i]);
                    }
                    adapter.Fill(dataSet, "myTable");
                    dt = dataSet.Tables["myTable"];
                    return true;
                }
                catch (Exception exception)
                {
                    this.strErro = sqlString + "\n" + exception.Message;
                    return false;
                }
            }
    
            public bool GetDataTable(string sqlString, int pageIndex, int maxRecords, ref DataTable DataTable)
            {
                try
                {
                    SQLiteConnection selectConnection = new SQLiteConnection(connString);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlString, selectConnection);
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, pageIndex, maxRecords, "myTable");
                    DataTable = dataSet.Tables["myTable"];
                    return true;
                }
                catch (Exception exception)
                {
                    this.strErro = sqlString + "\n" + exception.Message;
                    return false;
                }
            }
    
            public string GetOneValue(string sqlString, SQLiteParameter[] pa)
            {
                SQLiteCommand command = new SQLiteCommand(sqlString, new SQLiteConnection(connString));
                for (int i = 0; i < pa.Length; i++)
                {
                    command.Parameters.Add(pa[i]);
                }
                command.Connection.Open();
                string str = command.ExecuteScalar().ToString();
                command.Connection.Close();
                return str;
            }
    
            public string GetOneValue(string connString, string sqlString)
            {
                SQLiteCommand command = this.CreateCommand(sqlString);
                string str = command.ExecuteScalar().ToString();
                command.Connection.Close();
                return str;
            }
    
            private void Open(string _connectionString)
            {
                if (this.conn == null)
                {
                    this.conn = new SQLiteConnection(_connectionString);
                    this.conn.Open();
                }
                else if (this.conn.State != ConnectionState.Open)
                {
                    this.conn.Open();
                }
            }
    
            public void Close()
            {
                if (this.conn != null)
                {
                    this.conn.Close();
                }
                else if (this.conn.State == ConnectionState.Open)
                {
                    this.conn.Close();
                }
            }
    
            public void Dispose()
            {
                if (this.conn != null)
                {
                    this.conn.Dispose();
                    this.conn = null;
                }
            }
    
            public string ErrString
            {
                get
                {
                    return this.strErro;
                }
            }
    
            private string connString
            {
                get
                {
                    return this.connStr;
                }
                set
                {
                    this.connStr = value;
                }
            }
    
            public int TimeOut
            {
                get
                {
                    return this.iTimeOut;
                }
                set
                {
                    this.iTimeOut = value;
                }
            }
    
        }
    }

    版权说明:作者:张颖希PocketZ's Blog
    出处:http://www.cnblogs.com/PocketZ
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    若本文为翻译内容,目的为练习英文水平,如有雷同,纯属意外!有不妥之处,欢迎拍砖

  • 相关阅读:
    (转)tomcat与地址栏图标之研究(多浏览器)
    (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解
    (转) Tomcat部署Web应用方法总结
    (转)mysql账号权限密码设置方法
    (转) mysql的连接,创建账号,修改密码
    (转)mysql各个主要版本之间的差异
    (转)浅析Mysql的my.ini文件
    (转)如何在Windows上安装多个MySQL
    [笔记]线段树学习笔记
    [教程]对拍程序(linux)+ 考试(做题)生成数据 + 提交注意事项
  • 原文地址:https://www.cnblogs.com/PocketZ/p/1822611.html
Copyright © 2020-2023  润新知