4b1a4dadb9
Sono stati creati nuovi file e modifiche significative al progetto `BSHash`, inclusi file di configurazione, codice sorgente e risorse. È stato creato un file di soluzione `BSHash.sln` e un file di configurazione `App.config` per gestire le impostazioni utente. Il progetto `BSHash.csproj` è stato definito per specificare le proprietà e i riferimenti necessari. Sono stati implementati nuovi file sorgente, tra cui `Calculator.cs`, `Database.cs`, `Logger.cs`, `Network.cs`, `Storage.cs`, e `Main.cs`, che contengono la logica principale dell'applicazione. È stata aggiunta una nuova interfaccia utente in `Main.Designer.cs` e sono stati creati file di risorse `Resources.resx` e `Resources.Designer.cs`. È stato aggiunto un file `AssemblyInfo.cs` per le informazioni sull'assembly e implementato un sistema di gestione delle impostazioni utente tramite `Settings.settings` e `Settings.Designer.cs`. Infine, è stato creato `Program.cs` per avviare l'applicazione.
243 lines
8.3 KiB
C#
243 lines
8.3 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
|
|
namespace BSHash
|
|
{
|
|
public class Database
|
|
{
|
|
private readonly string _connectionString;
|
|
|
|
public Database()
|
|
{
|
|
string serverstring = Properties.Settings.Default.Server;
|
|
string databasestring = Properties.Settings.Default.Database;
|
|
string userstring = Properties.Settings.Default.User;
|
|
string passwordstring = Properties.Settings.Default.Password;
|
|
|
|
// Ricompongo la stringa di connessione
|
|
_connectionString = $"Server={serverstring};Database={databasestring};User Id={userstring};Password={passwordstring};";
|
|
}
|
|
|
|
public bool ClearHash()
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlTransaction transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
// Delete all rows from the Hash table
|
|
string deleteQuery = "DELETE FROM Hash";
|
|
using (SqlCommand deleteCommand = new SqlCommand(deleteQuery, connection, transaction))
|
|
{
|
|
deleteCommand.ExecuteNonQuery();
|
|
}
|
|
|
|
transaction.Commit();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool HashExists(string hash)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
|
|
// Controlla se l'hash esiste già nel database
|
|
string checkQuery = "SELECT COUNT(*) FROM Hash WHERE Hash = (@Hash)";
|
|
using (SqlCommand checkCommand = new SqlCommand(checkQuery, connection))
|
|
{
|
|
checkCommand.Parameters.AddWithValue("@Hash", hash);
|
|
int count = (int)checkCommand.ExecuteScalar();
|
|
|
|
return count != 0;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return true;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool InsertHash(string path, string hash)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlTransaction transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
// L'hash non esiste quindi lo inserisco
|
|
string insertQuery = "INSERT INTO Hash (Path, Hash) VALUES (@Path, @Hash)";
|
|
using (SqlCommand insertCommand = new SqlCommand(insertQuery, connection, transaction))
|
|
{
|
|
insertCommand.Parameters.AddWithValue("@Path", path);
|
|
insertCommand.Parameters.AddWithValue("@Hash", hash);
|
|
insertCommand.ExecuteNonQuery();
|
|
}
|
|
|
|
transaction.Commit();
|
|
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
return false;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool UpdateHash(string path, string hash)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlTransaction transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
// L'hash esiste quindi aggiorno la data di ultima scansione
|
|
string updateQuery = "UPDATE Hash SET Path = (@Path) WHERE Hash = (@Hash)";
|
|
using (SqlCommand updateCommand = new SqlCommand(updateQuery, connection, transaction))
|
|
{
|
|
updateCommand.Parameters.AddWithValue("@Path", path);
|
|
updateCommand.Parameters.AddWithValue("@Hash", hash);
|
|
updateCommand.ExecuteNonQuery();
|
|
}
|
|
|
|
transaction.Commit();
|
|
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
|
|
return false;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool InsertHistory(string hash)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlTransaction transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
|
|
// L'hash esiste quindi aggiorno la data di ultima scansione
|
|
string updateQuery = "INSERT INTO History (Hash) VALUES (@Hash)";
|
|
using (SqlCommand updateCommand = new SqlCommand(updateQuery, connection, transaction))
|
|
{
|
|
updateCommand.Parameters.AddWithValue("@Hash", hash);
|
|
updateCommand.ExecuteNonQuery();
|
|
}
|
|
|
|
transaction.Commit();
|
|
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
|
|
return false;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void InsertLog(string message, LogType type = LogType.INFO)
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlTransaction transaction = connection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string query = "INSERT INTO Log (Message, Type) VALUES (@Message, @Type)";
|
|
using (SqlCommand command = new SqlCommand(query, connection, transaction))
|
|
{
|
|
command.Parameters.AddWithValue("@Message", message);
|
|
command.Parameters.AddWithValue("@Type", type);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public DataTable GetHashes()
|
|
{
|
|
DataTable hashes = new DataTable();
|
|
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
string query = "SELECT * FROM Hash";
|
|
|
|
using (SqlCommand command = new SqlCommand(query, connection))
|
|
{
|
|
connection.Open();
|
|
using (SqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
hashes.Load(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
return hashes;
|
|
}
|
|
|
|
public DataTable GetLogs(string query)
|
|
{
|
|
DataTable logs = new DataTable();
|
|
|
|
using (SqlConnection connection = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand command = new SqlCommand(query, connection))
|
|
{
|
|
connection.Open();
|
|
using (SqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
logs.Load(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
return logs;
|
|
}
|
|
}
|
|
}
|