zoukankan      html  css  js  c++  java
  • c# 操作sqlite 数据库的类,

    c# 操作sqlite 数据库的类,绝对原创
    2010-05-29 17:31

    using System;
    using System.Text;
    using System.Data;
    using System.Data.SQLite;

    namespace WQ.SQLite.DataBase
    {

    //如果不会怎么调用,请联系开发作者QQ:13164946

    //作者:epe521 Eml:epe521@qq.com


        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;
                }
            }

        }
    }

    还有SQL操作类,功能绝对强大。

  • 相关阅读:
    C# Tips Written By Andrew Troelsen
    ASP.NET:性能与缓存
    New Feature In C# 2.0
    .NET Remoting中的通道注册
    通过应用程序域AppDomain加载和卸载程序集
    Some BrainTeaser in WinDev, Can you Solve them?
    ExtJs学习笔记(24)Drag/Drop拖动功能
    wap开发体会
    关于”System.ServiceModel.Activation.WebServiceHostFactory“与"<webHttp/>"以及RestFul/启用了Ajax的WCF服务
    验证码无刷新更换
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1763735.html
Copyright © 2011-2022 走看看