Subversion Repository Public Repository

ChrisCompleteCodeTrunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;

namespace Action_Tire_Payment_Processor
{
    public static class Settings
    {
        public static FileInfo ConfigFile;
        public static SQLiteConnection sql;
        public static bool ConfigFileRelative = bool.Parse(ConfigurationManager.AppSettings["Config Relative"]);
        public static string ConfigFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"BBDS Solutions\ATPP");//ConfigurationManager.AppSettings["Config File"];

        public static void Init()
        {
            if (!Directory.Exists(ConfigFilePath))
            {
                Directory.CreateDirectory(ConfigFilePath);
            }
            //if (ConfigFileRelative)
            //{
            string path = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "settings");
            //    ConfigFile = new FileInfo(path);
            //}
            //else
            //{
            if (File.Exists(path) && !File.Exists(Path.Combine(ConfigFilePath, "settings")))
            {
                File.Copy(path, Path.Combine(ConfigFilePath, "settings"));
            }

            ConfigFile = new FileInfo(Path.Combine(ConfigFilePath, "settings"));
            //}
                
            sql = new SQLiteConnection("Data Source=" + ConfigFile.FullName + ";Version=3;UseUTF16Encoding=True;");
            sql.ParseViaFramework = true;
            InitDB();
        }

        public static void InitDB()
        {
            try
            {
                string query = "CREATE TABLE IF NOT EXISTS Option(Name TEXT PRIMARY KEY, Value TEXT);";
                sql.Open();
                SQLiteCommand cmd = new SQLiteCommand(query, sql);
                cmd.ExecuteNonQuery();
                sql.Close();
            }
            catch (Exception ex)
            {
                try { sql.Close(); }
                catch { }
                throw ex;
            }

            sql.Open();
        }

        public static void SetOption(string name, string value)
        {
            string query = "INSERT OR REPLACE INTO Option (Name, Value) VALUES (@NAME, @VALUE);";
            try { sql.Open(); }
            catch { }
            //sql.Open();
            SQLiteCommand cmd = new SQLiteCommand(query, sql);
            cmd.Parameters.AddWithValue("@NAME", name);
            cmd.Parameters.AddWithValue("@VALUE", value);
            cmd.ExecuteNonQuery();
            //sql.Close();
        }

        public static string GetOption(string name)
        {
            string query = "SELECT * FROM Option WHERE Name = @NAME";
            try { sql.Open(); }
            catch { }
            //sql.Open();
            SQLiteCommand cmd = new SQLiteCommand(query, sql);
            cmd.Parameters.AddWithValue("@NAME", name);
            SQLiteDataReader reader = cmd.ExecuteReader();
            string value = "";
            while (reader.Read())
            {
                value = reader.GetString(1);
            }
            //sql.Close();

            return value;
        }

    }
}

Commits for ChrisCompleteCodeTrunk/ATTP/Action Tire Payment Processor/Settings.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000