Umstellung, app.conf auslesen um zu entscheiden welche db genutzt wird
This commit is contained in:
parent
8983561a2b
commit
036671a6cb
17
ProgrammList/ConfigManager/ConfigManager.cs
Normal file
17
ProgrammList/ConfigManager/ConfigManager.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System.Configuration;
|
||||||
|
|
||||||
|
namespace ProgrammList.ConfigManager {
|
||||||
|
internal class ConfigManager {
|
||||||
|
public static string GetSetting(string key) {
|
||||||
|
return ConfigurationManager.AppSettings[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetSetting(string key, string value) {
|
||||||
|
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||||
|
configuration.AppSettings.Settings[key].Value = value;
|
||||||
|
configuration.Save(ConfigurationSaveMode.Full, true);
|
||||||
|
ConfigurationManager.RefreshSection("appSettings");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using ProgrammList.ListPrograms;
|
||||||
using ProgrammList.ListPrograms;
|
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
static void Main(string[] args) {
|
public static void Main(string[] args) {
|
||||||
|
|
||||||
|
|
||||||
ListPrograms list = new ListPrograms();
|
ListPrograms list = new ListPrograms();
|
||||||
|
|
||||||
@ -15,4 +15,6 @@ class Program {
|
|||||||
Console.WriteLine("Searching for 64 bit");
|
Console.WriteLine("Searching for 64 bit");
|
||||||
list.createList(keyname2, "x64");
|
list.createList(keyname2, "x64");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,20 +1,43 @@
|
|||||||
using Microsoft.Win32;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using Microsoft.Win32;
|
||||||
using ProgrammList.sql;
|
using ProgrammList.sql;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
||||||
namespace ProgrammList.ListPrograms {
|
namespace ProgrammList.ListPrograms {
|
||||||
internal class ListPrograms {
|
internal class ListPrograms {
|
||||||
|
|
||||||
string prgm_path = Directory.GetCurrentDirectory() + "\\";
|
string prgm_path = Directory.GetCurrentDirectory() + "\\";
|
||||||
string[] keyvaluenames = { "DisplayName", "DisplayVersion", "InstallDate"};
|
string[] keyvaluenames = { "DisplayName", "DisplayVersion", "InstallDate" };
|
||||||
Mssql sql;
|
private SqlBase sql;
|
||||||
|
|
||||||
internal ListPrograms() {
|
internal ListPrograms(string sqlType) {
|
||||||
|
if (sqlType == null) {
|
||||||
|
Console.WriteLine("SQL Database not defined in app.conf, allowed types:");
|
||||||
|
Console.WriteLine("MYSQL");
|
||||||
|
Console.WriteLine("MARIADB");
|
||||||
|
Console.WriteLine("MSSQL");
|
||||||
|
Console.WriteLine("SQLITE");
|
||||||
|
throw new ArgumentNullException();
|
||||||
|
System.Environment.Exit(13);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sqlType.Equals("MYSQL", StringComparison.OrdinalIgnoreCase) || sqlType.Equals("MARIADB", StringComparison.OrdinalIgnoreCase)) {
|
||||||
|
sql = new Mysql();
|
||||||
|
}
|
||||||
|
else if (sqlType.Equals("MSSQL", StringComparison.OrdinalIgnoreCase)) {
|
||||||
sql = new Mssql();
|
sql = new Mssql();
|
||||||
}
|
}
|
||||||
|
else if (sqlType.Equals("SQLITE", StringComparison.OrdinalIgnoreCase)) {
|
||||||
|
string filename = ConfigManager.GetSetting("Filename");
|
||||||
|
if (!filename.IsNullOrEmpty()) {
|
||||||
|
sql = new Sqlite(prgm_path, filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal void DeleteOldData() {
|
internal void DeleteOldData() {
|
||||||
sql.checkTableExists();
|
sql.CheckTableExists();
|
||||||
sql.deleteOldData(Dns.GetHostName());
|
sql.DeleteOldData(Dns.GetHostName());
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void createList(string hkey, string bit) {
|
internal void createList(string hkey, string bit) {
|
||||||
|
8
ProgrammList/sql/DB.cs
Normal file
8
ProgrammList/sql/DB.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace ProgrammList.sql {
|
||||||
|
public enum DB {
|
||||||
|
MYSQL,
|
||||||
|
MARIADB,
|
||||||
|
SQLITE,
|
||||||
|
MSSQL
|
||||||
|
}
|
||||||
|
}
|
@ -3,13 +3,9 @@
|
|||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
|
|
||||||
namespace ProgrammList.sql {
|
namespace ProgrammList.sql {
|
||||||
internal class Mssql {
|
public class Mssql : SqlBaseAbstract {
|
||||||
string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" };
|
|
||||||
|
|
||||||
public SqlConnection Connection;
|
//private static Dbconnection instance;
|
||||||
string connstring;
|
|
||||||
|
|
||||||
//private static DbConnection instance;
|
|
||||||
public Mssql() {
|
public Mssql() {
|
||||||
var builder = new SqlConnectionStringBuilder {
|
var builder = new SqlConnectionStringBuilder {
|
||||||
DataSource = "localhost",
|
DataSource = "localhost",
|
||||||
@ -22,24 +18,9 @@ namespace ProgrammList.sql {
|
|||||||
connstring = builder.ToString();
|
connstring = builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Open() {
|
public void GetAllData() {
|
||||||
Connection = new SqlConnection(connstring);
|
Open(DB.MSSQL);
|
||||||
try {
|
var command = sqlcon.CreateCommand();
|
||||||
Connection.Open();
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
Console.Write(ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Close() {
|
|
||||||
Connection.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
internal void getAllData() {
|
|
||||||
Open();
|
|
||||||
var command = Connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT * FROM list";
|
command.CommandText = @"SELECT * FROM list";
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
using (var reader = command.ExecuteReader()) {
|
||||||
@ -49,9 +30,9 @@ namespace ProgrammList.sql {
|
|||||||
}
|
}
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
internal bool getSingleLine(string pcid, string program, string version) {
|
public bool GetSingleLine(string pcid, string program, string version) {
|
||||||
Open();
|
Open(DB.MSSQL);
|
||||||
var command = Connection.CreateCommand();
|
var command = sqlcon.CreateCommand();
|
||||||
command.CommandText = @"SELECT * FROM list where PCID like "
|
command.CommandText = @"SELECT * FROM list where PCID like "
|
||||||
+ pcid + " and DisplayName like "
|
+ pcid + " and DisplayName like "
|
||||||
+ program + " and DisplayVersion like " + version + ";";
|
+ program + " and DisplayVersion like " + version + ";";
|
||||||
@ -61,9 +42,9 @@ namespace ProgrammList.sql {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void checkTableExists() {
|
public void CheckTableExists() {
|
||||||
Open();
|
Open(DB.MSSQL);
|
||||||
var command = Connection.CreateCommand();
|
var command = sqlcon.CreateCommand();
|
||||||
command.CommandText = "select case when exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'prgmlist' AND TABLE_NAME = 'list') then 1 else 0 end";
|
command.CommandText = "select case when exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'prgmlist' AND TABLE_NAME = 'list') then 1 else 0 end";
|
||||||
var returncode = command.ExecuteScalar();
|
var returncode = command.ExecuteScalar();
|
||||||
if (returncode != null) {
|
if (returncode != null) {
|
||||||
@ -89,9 +70,9 @@ namespace ProgrammList.sql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal void InsertData(Dictionary<string, string> valuesqlCommand) {
|
public void InsertData(Dictionary<string, string> valuesqlCommand) {
|
||||||
Open();
|
Open(DB.MSSQL);
|
||||||
var transaction = Connection.BeginTransaction();
|
var transaction = sqlcon.BeginTransaction();
|
||||||
|
|
||||||
string result = "";
|
string result = "";
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
@ -107,15 +88,15 @@ namespace ProgrammList.sql {
|
|||||||
|
|
||||||
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
||||||
|
|
||||||
var command = new SqlCommand(sqlCommand, Connection, transaction);
|
var command = new SqlCommand(sqlCommand, sqlcon, transaction);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
Console.WriteLine(sqlCommand);
|
Console.WriteLine(sqlCommand);
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void InsertOrUpdateData(Dictionary<string, string> value) {
|
public void InsertOrUpdateData(Dictionary<string, string> value) {
|
||||||
if (getSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
if (GetSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
||||||
UpdateData(value);
|
UpdateData(value);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -123,18 +104,18 @@ namespace ProgrammList.sql {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void deleteOldData(string hostname) {
|
public void DeleteOldData(string hostname) {
|
||||||
Open();
|
Open(DB.MSSQL);
|
||||||
var command = Connection.CreateCommand();
|
var command = sqlcon.CreateCommand();
|
||||||
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
||||||
command.CommandText = sqlCommand;
|
command.CommandText = sqlCommand;
|
||||||
command.ExecuteReader();
|
command.ExecuteReader();
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void UpdateData(Dictionary<string, string> value) {
|
public void UpdateData(Dictionary<string, string> value) {
|
||||||
Open();
|
Open(DB.MSSQL);
|
||||||
var transaction = Connection.BeginTransaction();
|
var transaction = sqlcon.BeginTransaction();
|
||||||
string sqlCommand = @"Update list ";
|
string sqlCommand = @"Update list ";
|
||||||
|
|
||||||
string result = "set ";
|
string result = "set ";
|
||||||
@ -152,7 +133,7 @@ namespace ProgrammList.sql {
|
|||||||
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
||||||
|
|
||||||
|
|
||||||
var command = new SqlCommand(sqlCommand, Connection, transaction);
|
var command = new SqlCommand(sqlCommand, sqlcon, transaction);
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
if (valuenames[i] != "PCID") {
|
if (valuenames[i] != "PCID") {
|
||||||
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
||||||
@ -163,19 +144,5 @@ namespace ProgrammList.sql {
|
|||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void DeleteData(string id) {
|
|
||||||
Open();
|
|
||||||
var command = Connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT name FROM user WHERE id = $id";
|
|
||||||
command.Parameters.AddWithValue("$id", id);
|
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
|
||||||
while (reader.Read()) {
|
|
||||||
var name = reader.GetString(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,47 +1,14 @@
|
|||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using System.Data.Common;
|
|
||||||
|
|
||||||
namespace ProgrammList.sql {
|
namespace ProgrammList.sql {
|
||||||
|
|
||||||
|
|
||||||
public class Mysql {
|
internal class Mysql : SqlBaseAbstract {
|
||||||
|
|
||||||
string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" };
|
|
||||||
|
|
||||||
public MySqlConnection Connection;
|
|
||||||
|
|
||||||
//private static DbConnection instance;
|
|
||||||
public Mysql() {
|
|
||||||
//string CnnStr = "Data Source=local;Initial Catalog=programlist;User Id=prgmlist;pwd=G0KaUM7TzgO7ZoPZCifs";
|
|
||||||
// instance = new MySqlConnection(CnnStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Open() {
|
|
||||||
string connstring = string.Format("Server={0}; database={1}; UID={2}; password={3}", "localhost", "programlist", "prgmlist", "G0KaUM7TzgO7ZoPZCifs");
|
|
||||||
Connection = new MySqlConnection(connstring);
|
|
||||||
Connection.Open();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Close() {
|
|
||||||
Connection.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
internal void getAllData() {
|
public Boolean GetSingleLine(string pcid, string program, string version) {
|
||||||
Open();
|
Open(DB.MYSQL);
|
||||||
var command = Connection.CreateCommand();
|
var command = mysqlcon.CreateCommand();
|
||||||
command.CommandText = @"SELECT * FROM list";
|
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
|
||||||
while (reader.Read()) {
|
|
||||||
var dataLine = reader.GetString(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
internal Boolean getSingleLine(string pcid, string program, string version) {
|
|
||||||
Open();
|
|
||||||
var command = Connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT * FROM list where PCID like "
|
command.CommandText = @"SELECT * FROM list where PCID like "
|
||||||
+ pcid + " and DisplayName like "
|
+ pcid + " and DisplayName like "
|
||||||
+ program + " and DisplayVersion like " + version + ";";
|
+ program + " and DisplayVersion like " + version + ";";
|
||||||
@ -51,9 +18,9 @@ namespace ProgrammList.sql {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void checkTableExists() {
|
public void CheckTableExists() {
|
||||||
Open();
|
Open(DB.MYSQL);
|
||||||
var command = Connection.CreateCommand();
|
var command = mysqlcon.CreateCommand();
|
||||||
command.CommandText = @"SHOW TABLES LIKE 'list';";
|
command.CommandText = @"SHOW TABLES LIKE 'list';";
|
||||||
var name = command.ExecuteScalar();
|
var name = command.ExecuteScalar();
|
||||||
if (name != null && name.ToString() == "list") {
|
if (name != null && name.ToString() == "list") {
|
||||||
@ -67,9 +34,9 @@ namespace ProgrammList.sql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal void InsertData(Dictionary<string, string> valuesqlCommand) {
|
public void InsertData(Dictionary<string, string> valuesqlCommand) {
|
||||||
Open();
|
Open(DB.MYSQL);
|
||||||
var transaction = Connection.BeginTransaction();
|
var transaction = mysqlcon.BeginTransaction();
|
||||||
|
|
||||||
string result = "";
|
string result = "";
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
@ -85,15 +52,15 @@ namespace ProgrammList.sql {
|
|||||||
|
|
||||||
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
||||||
|
|
||||||
var command = new MySqlCommand(sqlCommand, Connection, transaction);
|
var command = new MySqlCommand(sqlCommand, mysqlcon, transaction);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
Console.WriteLine(sqlCommand);
|
Console.WriteLine(sqlCommand);
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void InsertOrUpdateData(Dictionary<string, string> value) {
|
public void InsertOrUpdateData(Dictionary<string, string> value) {
|
||||||
if (getSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
if (GetSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
||||||
UpdateData(value);
|
UpdateData(value);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -101,18 +68,9 @@ namespace ProgrammList.sql {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void deleteOldData(string hostname) {
|
public void UpdateData(Dictionary<string, string> value) {
|
||||||
Open();
|
Open(DB.MYSQL);
|
||||||
var command = Connection.CreateCommand();
|
var transaction = mysqlcon.BeginTransaction();
|
||||||
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
|
||||||
command.CommandText = sqlCommand;
|
|
||||||
command.ExecuteReader();
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void UpdateData(Dictionary<string, string> value) {
|
|
||||||
Open();
|
|
||||||
var transaction = Connection.BeginTransaction();
|
|
||||||
string sqlCommand = @"Update list ";
|
string sqlCommand = @"Update list ";
|
||||||
|
|
||||||
string result = "set ";
|
string result = "set ";
|
||||||
@ -130,7 +88,7 @@ namespace ProgrammList.sql {
|
|||||||
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
||||||
|
|
||||||
|
|
||||||
var command = new MySqlCommand(sqlCommand, Connection, transaction);
|
var command = new MySqlCommand(sqlCommand, mysqlcon, transaction);
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
if (valuenames[i] != "PCID") {
|
if (valuenames[i] != "PCID") {
|
||||||
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
||||||
@ -142,18 +100,14 @@ namespace ProgrammList.sql {
|
|||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void DeleteData(string id) {
|
|
||||||
Open();
|
|
||||||
var command = Connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT name FROM user WHERE id = $id";
|
|
||||||
command.Parameters.AddWithValue("$id", id);
|
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
public void DeleteOldData(string hostname) {
|
||||||
while (reader.Read()) {
|
Open(DB.MYSQL);
|
||||||
var name = reader.GetString(0);
|
var command = mysqlcon.CreateCommand();
|
||||||
}
|
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
||||||
}
|
command.CommandText = sqlCommand;
|
||||||
|
command.ExecuteReader();
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,162 +1,21 @@
|
|||||||
using Microsoft.Data.SqlClient;
|
namespace ProgrammList.sql {
|
||||||
using Microsoft.Data.Sqlite;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Transactions;
|
|
||||||
using System.Xml.Linq;
|
|
||||||
|
|
||||||
namespace ProgrammList.sql {
|
interface SqlBase {
|
||||||
|
|
||||||
/**
|
|
||||||
* Basic commands to connect to the Sqlight database
|
|
||||||
*/
|
|
||||||
internal class SqlBase {
|
|
||||||
|
|
||||||
SqliteConnection connection;
|
internal bool GetSingleLine(string pcid, string program, string version);
|
||||||
string prgm_path;
|
|
||||||
string filename;
|
|
||||||
|
|
||||||
string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" };
|
internal void CheckTableExists();
|
||||||
|
|
||||||
/**
|
internal void InsertData(Dictionary<string, string> valuesqlCommand);
|
||||||
* Constructor
|
|
||||||
*
|
internal void InsertOrUpdateData(Dictionary<string, string> value);
|
||||||
*/
|
|
||||||
internal SqlBase(string prm_path, string file) {
|
internal void DeleteOldData(string hostname);
|
||||||
|
|
||||||
|
internal void UpdateData(Dictionary<string, string> value);
|
||||||
|
|
||||||
prgm_path = prm_path + filename;
|
|
||||||
filename = file;
|
|
||||||
|
|
||||||
//connection = new SqliteConnection("Data Source=" + prm_path + filename + " ;PRAGMA journal_mode=WAL;");
|
|
||||||
connection = new SqliteConnection("Data Source=" + prm_path + filename);
|
|
||||||
connection.Open();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void getAllData() {
|
|
||||||
var command = connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT * FROM list";
|
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
|
||||||
while (reader.Read()) {
|
|
||||||
var dataLine = reader.GetString(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
internal Boolean getSingleLine(string pcid, string program, string version) {
|
|
||||||
var command = connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT * FROM list where PCID like "
|
|
||||||
+ pcid + " and DisplayName like "
|
|
||||||
+ program + " and DisplayVersion like " + version + ";";
|
|
||||||
|
|
||||||
|
|
||||||
bool result = command.ExecuteReader().Read();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void checkTableExists() {
|
|
||||||
var command = connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT name FROM sqlite_master WHERE type='table' AND name='list';";
|
|
||||||
var name = command.ExecuteScalar();
|
|
||||||
if (name != null && name.ToString() == "list") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var cols = string.Join(" VARCHAR,", valuenames);
|
|
||||||
cols = cols + " Varchar";
|
|
||||||
command.CommandText = "CREATE TABLE list (" +cols + ")";
|
|
||||||
command.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
internal void InsertData(Dictionary<string, string> valuesqlCommand) {
|
|
||||||
var transaction = connection.BeginTransaction();
|
|
||||||
|
|
||||||
string result = "";
|
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
|
||||||
result += valuesqlCommand.GetValueOrDefault(valuenames[i]);
|
|
||||||
|
|
||||||
if (i < valuenames.Length -1) {
|
|
||||||
result += ",";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var cols = String.Join(",", valuenames);
|
|
||||||
|
|
||||||
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
|
||||||
|
|
||||||
var command = new SqliteCommand(sqlCommand, connection, transaction);
|
|
||||||
Console.WriteLine(sqlCommand);
|
|
||||||
command.ExecuteNonQuery();
|
|
||||||
transaction.Commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void InsertOrUpdateData(Dictionary<string, string> value) {
|
|
||||||
if (getSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
|
||||||
|
|
||||||
Console.WriteLine("Update");
|
|
||||||
UpdateData(value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
Console.WriteLine("Insert");
|
|
||||||
InsertData(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void deleteOldData(string hostname) {
|
|
||||||
var command = connection.CreateCommand();
|
|
||||||
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
|
||||||
command.CommandText = sqlCommand;
|
|
||||||
command.ExecuteReader();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void UpdateData(Dictionary<string, string> value) {
|
|
||||||
var transaction = connection.BeginTransaction();
|
|
||||||
string sqlCommand = @"Update list ";
|
|
||||||
|
|
||||||
string result = "set ";
|
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
|
||||||
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]);
|
|
||||||
|
|
||||||
if (i < valuenames.Length - 1) {
|
|
||||||
result += " ,";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlCommand = sqlCommand + result;
|
|
||||||
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") +
|
|
||||||
" and DisplayName like " + value.GetValueOrDefault("DisplayName") +
|
|
||||||
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
|
||||||
|
|
||||||
|
|
||||||
var command = new SqliteCommand(sqlCommand, connection, transaction);
|
|
||||||
for (int i = 0; i < valuenames.Length; i++) {
|
|
||||||
if (valuenames[i] != "PCID") {
|
|
||||||
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine(command.CommandText);
|
|
||||||
command.ExecuteNonQuery();
|
|
||||||
transaction.Commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void DeleteData(string id) {
|
|
||||||
var command = connection.CreateCommand();
|
|
||||||
command.CommandText = @"SELECT name FROM user WHERE id = $id";
|
|
||||||
command.Parameters.AddWithValue("$id", id);
|
|
||||||
|
|
||||||
using (var reader = command.ExecuteReader()) {
|
|
||||||
while (reader.Read()) {
|
|
||||||
var name = reader.GetString(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
86
ProgrammList/sql/SqlBaseAbstract.cs
Normal file
86
ProgrammList/sql/SqlBaseAbstract.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
namespace ProgrammList.sql {
|
||||||
|
|
||||||
|
public abstract class SqlBaseAbstract : SqlBase {
|
||||||
|
|
||||||
|
|
||||||
|
public string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" };
|
||||||
|
public string connstring = null;
|
||||||
|
public MySqlConnection mysqlcon = null;
|
||||||
|
public SqlConnection sqlcon = null;
|
||||||
|
public SqliteConnection sqlitecon = null;
|
||||||
|
|
||||||
|
public void Close() {
|
||||||
|
if (mysqlcon != null) {
|
||||||
|
mysqlcon.Close();
|
||||||
|
}
|
||||||
|
else if (sqlcon != null) {
|
||||||
|
sqlcon.Close();
|
||||||
|
}
|
||||||
|
else if (sqlitecon != null {
|
||||||
|
sqlitecon.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Open(DB type) {
|
||||||
|
switch (type) {
|
||||||
|
case DB.MYSQL:
|
||||||
|
case DB.MARIADB:
|
||||||
|
mysqlcon = new MySqlConnection(connstring);
|
||||||
|
try {
|
||||||
|
mysqlcon.Open();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
Console.Write(ex.ToString());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DB.MSSQL:
|
||||||
|
sqlcon = new SqlConnection(connstring);
|
||||||
|
try {
|
||||||
|
mysqlcon.Open();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
Console.Write(ex.ToString());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DB.SQLITE:
|
||||||
|
sqlitecon = new SqliteConnection(connstring);
|
||||||
|
try {
|
||||||
|
mysqlcon.Open();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
Console.Write(ex.ToString());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SqlBase.GetSingleLine(string pcid, string program, string version) {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SqlBase.CheckTableExists() {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SqlBase.InsertData(Dictionary<string, string> valuesqlCommand) {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SqlBase.InsertOrUpdateData(Dictionary<string, string> value) {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SqlBase.DeleteOldData(string hostname) {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SqlBase.UpdateData(Dictionary<string, string> value) {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
130
ProgrammList/sql/Sqlite.cs
Normal file
130
ProgrammList/sql/Sqlite.cs
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
namespace ProgrammList.sql {
|
||||||
|
public class Sqlite : SqlBaseAbstract {
|
||||||
|
|
||||||
|
string filename;
|
||||||
|
|
||||||
|
public Sqlite(string prm_path, string file) {
|
||||||
|
|
||||||
|
string prgm_path = prm_path + filename;
|
||||||
|
filename = file;
|
||||||
|
|
||||||
|
sqlitecon = new SqliteConnection("Data Source=" + prm_path + filename);
|
||||||
|
sqlitecon.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool GetSingleLine(string pcid, string program, string version) {
|
||||||
|
var command = sqlitecon.CreateCommand();
|
||||||
|
command.CommandText = @"SELECT * FROM list where PCID like "
|
||||||
|
+ pcid + " and DisplayName like "
|
||||||
|
+ program + " and DisplayVersion like " + version + ";";
|
||||||
|
|
||||||
|
|
||||||
|
bool result = command.ExecuteReader().Read();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CheckTableExists() {
|
||||||
|
var command = sqlitecon.CreateCommand();
|
||||||
|
command.CommandText = @"SELECT name FROM sqlite_master WHERE type='table' AND name='list';";
|
||||||
|
var name = command.ExecuteScalar();
|
||||||
|
if (name != null && name.ToString() == "list") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var cols = string.Join(" VARCHAR,", valuenames);
|
||||||
|
cols = cols + " Varchar";
|
||||||
|
command.CommandText = "CREATE TABLE list (" + cols + ")";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void InsertData(Dictionary<string, string> valuesqlCommand) {
|
||||||
|
var transaction = sqlitecon.BeginTransaction();
|
||||||
|
|
||||||
|
string result = "";
|
||||||
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
|
result += valuesqlCommand.GetValueOrDefault(valuenames[i]);
|
||||||
|
|
||||||
|
if (i < valuenames.Length - 1) {
|
||||||
|
result += ",";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var cols = String.Join(",", valuenames);
|
||||||
|
|
||||||
|
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
||||||
|
SqliteConnection con;
|
||||||
|
var command = new SqliteCommand(sqlCommand, sqlitecon, transaction);
|
||||||
|
Console.WriteLine(sqlCommand);
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertOrUpdateData(Dictionary<string, string> value) {
|
||||||
|
if (GetSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
||||||
|
|
||||||
|
Console.WriteLine("Update");
|
||||||
|
UpdateData(value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
Console.WriteLine("Insert");
|
||||||
|
InsertData(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteOldData(string hostname) {
|
||||||
|
var command = sqlitecon.CreateCommand();
|
||||||
|
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
||||||
|
command.CommandText = sqlCommand;
|
||||||
|
command.ExecuteReader();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateData(Dictionary<string, string> value) {
|
||||||
|
var transaction = sqlitecon.BeginTransaction();
|
||||||
|
string sqlCommand = @"Update list ";
|
||||||
|
|
||||||
|
string result = "set ";
|
||||||
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
|
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]);
|
||||||
|
|
||||||
|
if (i < valuenames.Length - 1) {
|
||||||
|
result += " ,";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlCommand = sqlCommand + result;
|
||||||
|
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") +
|
||||||
|
" and DisplayName like " + value.GetValueOrDefault("DisplayName") +
|
||||||
|
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
||||||
|
|
||||||
|
|
||||||
|
var command = new SqliteCommand(sqlCommand, sqlitecon, transaction);
|
||||||
|
for (int i = 0; i < valuenames.Length; i++) {
|
||||||
|
if (valuenames[i] != "PCID") {
|
||||||
|
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine(command.CommandText);
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteData(string id) {
|
||||||
|
var command = sqlitecon.CreateCommand();
|
||||||
|
command.CommandText = @"SELECT name FROM user WHERE id = $id";
|
||||||
|
command.Parameters.AddWithValue("$id", id);
|
||||||
|
|
||||||
|
using (var reader = command.ExecuteReader()) {
|
||||||
|
while (reader.Read()) {
|
||||||
|
var name = reader.GetString(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user