SQLite.cs 3.4 KB
using System;
using System.Collections.Generic;
using System.Data.SQLite;

namespace DAL
{
    public class SQLite
    {
        private SQLiteConnection con;

        public SQLite()
        {
        }

        public bool IsCon { private set; get; } = false;

        //public bool Create()
        //{
        //    try
        //    {
        //        con = new SQLiteConnection("data source=" + Model.FilePath.CONFIG_DATABASE);
        //        SQLiteCommand cmd = new(con);
        //        con.Open();
        //        cmd.CommandText = "CREATE TABLE SN ([Text] STRING NOT NULL UNIQUE, [Count] INT NOT NULL)";
        //        cmd.ExecuteNonQuery();
        //        cmd.CommandText = "CREATE TABLE History ([QRCode] STRING, [LabelName] STRING, [BarCode] STRING, [PrintKey] STRING, [CreateTime] STRING)";
        //        cmd.ExecuteNonQuery();

        //        cmd.Dispose();
        //        con.Close();
        //        con.Dispose();

        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        return false;
        //    }
        //}

        public bool Connect()
        {
            try
            {
                con = new SQLiteConnection("data source='" + Model.FilePath.CONFIG_DATABASE + "';Version=3");
                Model.LogNet.log.Debug($"SQLite ConnectionString = {con.ConnectionString}");
                con.Open();
                IsCon = true;
                Model.LogNet.log.Debug("SQLite数据库连接成功");
            }
            catch (Exception ex)
            {
                IsCon = false;
                Model.LogNet.log.Error("数据库连接失败", ex);
            }
            return IsCon;
        }

        public void Close()
        {
            if (con != null)
            {
                con.Close();
                con.Dispose();
            }
            Model.LogNet.log.Debug("SQLite数据库关闭");
        }

        public bool Select(string sql, out string[][] data)
        {
            data = null;
            if (!IsCon) return false;
            List<string[]> content = new();

            try
            {
                SQLiteCommand cmd = new(con) { CommandText = sql };
                SQLiteDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string[] s = new string[dr.FieldCount];
                    for (int i = 0; i < s.Length; i++)
                        s[i] = dr[i].ToString();
                    content.Add(s);
                }
                dr.Close();
                cmd.Dispose();
                data = content.ToArray();
                Model.LogNet.log.Debug("Select,SQL = " + sql);
                return true;
            }
            catch (Exception ex)
            {
                Model.LogNet.log.Error("Select", ex);
                return false;
            }

        }

        public bool Execute(string sql)
        {
            if (!IsCon) return false;

            try
            {
                SQLiteCommand cmd = new(con) { CommandText = sql };
                int n = cmd.ExecuteNonQuery();
                cmd.Dispose();
                Model.LogNet.log.Info("Execute,SQL = " + sql);
                return true;
            }
            catch (Exception ex)
            {
                Model.LogNet.log.Error("Execute", ex);
                return false;
            }
        }

    }
}