DB Browser SQLite 操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;
using System.Reflection;
using System.Diagnostics;

namespace DB_Browser_SQLite
{
    class SqliteManager
    {
        public SQLiteConnection Connection = null;

        protected bool isConnected
        {
            get { return _isConnected; }
        }

        private bool _isConnected = false;
        private object _obj = new object();

        protected virtual bool ConnectDatabase(String connectionString)
        {
            try
            {
                if (this.Connection != null)
                {
                    this.Connection.Close();
                    this.Connection.Dispose();
                    this.Connection = null;
                }

                this.Connection = new SQLiteConnection(connectionString);
                this.Connection.Open();

                if (this.Connection.State == System.Data.ConnectionState.Open)
                    _isConnected = true;
                else
                    _isConnected = false;

                return _isConnected;
            }
            catch
            {
                Debug.WriteLine("Display Error Message.. ConnectSQLite");
                return false;
            }
        }

        protected int ExecuteNonQuery(String query)
        {
            if (_isConnected == false)
                return -1;

            try
            {
                SQLiteCommand cmd = null;
                cmd = new SQLiteCommand(query, this.Connection);

                return cmd.ExecuteNonQuery();
            }
            catch
            {
                return -1;
            }
        }

        /// 
        /// Database插入 Binary数据,图片和文件使用Insert
        /// 
        ///  "@Image1"
        /// byte[] ...
        protected void ExecuteNonQuery(String query, params object[] parameters)
        {
            if (_isConnected == false)
                return;

            try
            {
                SQLiteCommand cmd = null;
                cmd = new SQLiteCommand(query, this.Connection);

                for (int i = 0; i < parameters.Length; i += 2)
                {
                    SQLiteParameter parameter = new SQLiteParameter(parameters[i].ToString(), parameters[i + 1]);
                    cmd.Parameters.Add(parameter);
                }

                cmd.ExecuteNonQuery();
            }
            catch
            {
                return;
            }
        }

        protected SQLiteDataReader ExecuteReader(String query)
        {
            if (_isConnected == false)
                return null;

            try
            {
                SQLiteCommand cmd = null;
                cmd = new SQLiteCommand(query, this.Connection);

                SQLiteDataReader reader = cmd.ExecuteReader();
                return reader;
            }
            catch
            {
                return null;
            }
        }

        protected DataSet ExecuteAdpater(String query, DataSet ds)
        {
            if (_isConnected == false)
                return null;

            try
            {
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, this.Connection);
                adapter.Fill(ds);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        protected IList Map(SQLiteDataReader reader) where T : class, new()
        {
            IList collection = new List();
            while (reader.Read())
            {
                T obj = new T();
                foreach (var property in obj.GetType().GetProperties()
                    .Where(p => p.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(DatabaseColumnAttribute)) != null).ToList())
                {
                    try
                    {
                        var columnAttribute = property.GetCustomAttribute(typeof(DatabaseColumnAttribute)) as DatabaseColumnAttribute;

                        if (columnAttribute != null)
                        {
                            if (columnAttribute.Convert == true)
                            {
                                if (reader[property.Name] != DBNull.Value)
                                    property.SetValue(obj, Convert.ChangeType(reader[property.Name], property.PropertyType));
                            }
                            else
                            {
                                if (reader.HasRows)
                                {
                                    if (reader[property.Name] != DBNull.Value)
                                        property.SetValue(obj, reader[property.Name]);
                                }
                            }
                        }
                    }
                    catch
                    {

                    }
                }

                collection.Add(obj);
            }

            return collection;
        }

        protected IList ExecuteGet(string cmdText) where T : class, new()
        {
            SQLiteCommand cmd = new SQLiteCommand(cmdText, this.Connection);
            using (var reader = cmd.ExecuteReader())
            {
                return Map(reader);
            }
        }

        protected IList ExecuteGet(SQLiteDataReader reader) where T : class, new()
        {
            return Map(reader);
        }
    }

    public class DatabaseColumnAttribute : Attribute
    {
        public bool Convert { get; set; }

        public bool IsPrimary { get; set; }

        public bool IsIndex { get; set; }

        public bool IsCondition { get; set; }
    }

    public class DatabaseTableAttribute : Attribute
    {
        public bool IsTable { get; set; }
    }
}

 

你可能感兴趣的:(Dot,Net(C#))