2025-03-05 00:31:22 +01:00
using Microsoft.Data.SqlClient ;
namespace ProgrammList.sql {
2025-03-07 23:36:20 +01:00
public class Mssql : SqlBase {
public string [ ] valuenames = { "PCID" , "DisplayName" , "DisplayVersion" , "InstallDate" , "update_date" , "APP_Architecture" } ;
public string connstring = null ;
string constring = null ;
SqlConnection mssqlcon = null ;
2025-03-05 00:31:22 +01:00
2025-03-05 23:12:50 +01:00
//private static Dbconnection instance;
2025-03-05 00:31:22 +01:00
public Mssql ( ) {
var builder = new SqlConnectionStringBuilder {
DataSource = "localhost" ,
UserID = "sa" ,
Password = "2677890E23" ,
InitialCatalog = "prgmlist" ,
TrustServerCertificate = true
} ;
2025-03-07 23:36:20 +01:00
constring = builder . ToString ( ) ;
}
public void Open ( ) {
mssqlcon . Open ( ) ;
}
public void Close ( ) {
mssqlcon . Close ( ) ;
2025-03-05 00:31:22 +01:00
}
2025-03-05 23:12:50 +01:00
public void GetAllData ( ) {
2025-03-07 23:36:20 +01:00
Open ( ) ;
var command = mssqlcon . CreateCommand ( ) ;
2025-03-05 00:31:22 +01:00
command . CommandText = @"SELECT * FROM list" ;
using ( var reader = command . ExecuteReader ( ) ) {
while ( reader . Read ( ) ) {
var dataLine = reader . GetString ( 0 ) ;
}
}
Close ( ) ;
}
2025-03-05 23:12:50 +01:00
public bool GetSingleLine ( string pcid , string program , string version ) {
2025-03-07 23:36:20 +01:00
Open ( ) ;
var command = mssqlcon . CreateCommand ( ) ;
2025-03-05 00:31:22 +01:00
command . CommandText = @"SELECT * FROM list where PCID like "
+ pcid + " and DisplayName like "
+ program + " and DisplayVersion like " + version + ";" ;
bool result = command . ExecuteReader ( ) . Read ( ) ;
Close ( ) ;
return result ;
}
2025-03-05 23:12:50 +01:00
public void CheckTableExists ( ) {
2025-03-07 23:36:20 +01:00
Open ( ) ;
var command = mssqlcon . CreateCommand ( ) ;
2025-03-05 21:24:52 +01:00
command . CommandText = "select case when exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'prgmlist' AND TABLE_NAME = 'list') then 1 else 0 end" ;
2025-03-05 00:31:22 +01:00
var returncode = command . ExecuteScalar ( ) ;
2025-03-05 21:24:52 +01:00
if ( returncode ! = null ) {
int rc ;
bool convertingResult = Int32 . TryParse ( returncode . ToString ( ) , out rc ) ;
if ( ! convertingResult ) {
throw new FormatException ( ) ;
}
if ( rc > 0 ) {
Console . WriteLine ( "Creating table..." ) ;
}
else {
return ;
}
2025-03-05 00:31:22 +01:00
}
var cols = string . Join ( " VARCHAR(255)," , valuenames ) ;
cols = cols + " Varchar(255)" ;
command . CommandText = "CREATE TABLE list (" + cols + ")" ;
command . ExecuteNonQuery ( ) ;
Close ( ) ;
}
2025-03-05 23:12:50 +01:00
public void InsertData ( Dictionary < string , string > valuesqlCommand ) {
2025-03-07 23:36:20 +01:00
Open ( ) ;
var transaction = mssqlcon . BeginTransaction ( ) ;
2025-03-05 00:31:22 +01:00
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 + ")" ;
2025-03-07 23:36:20 +01:00
var command = new SqlCommand ( sqlCommand , mssqlcon , transaction ) ;
2025-03-05 00:31:22 +01:00
command . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
Console . WriteLine ( sqlCommand ) ;
Close ( ) ;
}
2025-03-05 23:12:50 +01:00
public void InsertOrUpdateData ( Dictionary < string , string > value ) {
if ( GetSingleLine ( value . GetValueOrDefault ( "PCID" ) , value . GetValueOrDefault ( "DisplayName" ) , value . GetValueOrDefault ( "DisplayVersion" ) ) ) {
2025-03-05 00:31:22 +01:00
UpdateData ( value ) ;
}
else {
InsertData ( value ) ;
}
}
2025-03-05 23:12:50 +01:00
public void DeleteOldData ( string hostname ) {
2025-03-07 23:36:20 +01:00
Open ( ) ;
var command = mssqlcon . CreateCommand ( ) ;
2025-03-05 00:31:22 +01:00
string sqlCommand = @"delete from list where PCID = '" + hostname + "';" ;
command . CommandText = sqlCommand ;
command . ExecuteReader ( ) ;
Close ( ) ;
}
2025-03-05 23:12:50 +01:00
public void UpdateData ( Dictionary < string , string > value ) {
2025-03-07 23:36:20 +01:00
Open ( ) ;
var transaction = mssqlcon . BeginTransaction ( ) ;
2025-03-05 00:31:22 +01:00
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" ) ;
2025-03-07 23:36:20 +01:00
var command = new SqlCommand ( sqlCommand , mssqlcon , transaction ) ;
2025-03-05 00:31:22 +01:00
for ( int i = 0 ; i < valuenames . Length ; i + + ) {
if ( valuenames [ i ] ! = "PCID" ) {
command . Parameters . AddWithValue ( "$" + valuenames [ i ] , value . GetValueOrDefault ( valuenames [ i ] ) ) ;
}
}
Console . WriteLine ( sqlCommand ) ;
command . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
Close ( ) ;
}
}
}