Class SQLiteConnectionManager
Namespace: TecnoPack.SQLite
Assembly: CO0002_Templ_FlowpackHMI_V00_26_M.dll
Manages a SQLite database connection and its associated commands, providing asynchronous access, transaction support, and safe resource disposal.
public class SQLiteConnectionManager
Inheritance
object ← SQLiteConnectionManager
Remarks
This class is designed to be used as a lightweight data access layer for embedded and industrial systems (e.g. OEE, PLC gateways).
A single instance may be reused for the lifetime of the application or scoped to a specific operation. The class is thread-safe for command registration and removal.
Constructors
SQLiteConnectionManager(string)
Initializes a new instance of the
public SQLiteConnectionManager(string connectionString)
Parameters
connectionString string
The SQLite connection string used to open the database.
Properties
IsConnectionOpen
Gets a value indicating whether the underlying SQLite connection is currently open.
public bool IsConnectionOpen { get; }
Property Value
bool
Methods
AddCommandAsync(string, string)
Creates and registers a reusable SQL command associated with this connection.
public Task<SqliteCommand> AddCommandAsync(string key, string sql)
Parameters
key string
A unique key used to identify and retrieve the command later.
sql string
The SQL statement to associate with the command.
Returns
Task<SqliteCommand>
The created
Remarks
If the connection is not open, it will be opened automatically.
AddParameter(SqliteCommand, string, object)
Adds a parameter to a SQLite command.
public SqliteCommand AddParameter(SqliteCommand cmd, string paramName, object value)
Parameters
cmd SqliteCommand
The command to which the parameter will be added.
paramName string
The parameter name.
value object
The parameter value. If null,
Returns
SqliteCommand
The same
BeginTransactionAsync()
Begins a new database transaction asynchronously.
public Task<SqliteTransaction> BeginTransactionAsync()
Returns
Task<SqliteTransaction>
A
Remarks
If the connection is not open, it will be opened automatically.
CloseConnectionAsync()
Closes the SQLite connection asynchronously and disposes all registered commands.
public Task CloseConnectionAsync()
Returns
Task
Remarks
This method disposes all tracked
CommitTransactionAsync(SqliteTransaction)
Commits a transaction and disposes it.
public Task CommitTransactionAsync(SqliteTransaction tx)
Parameters
tx SqliteTransaction
The transaction to commit.
Returns
Task
DisposeAsync()
Asynchronously disposes the connection manager and releases all resources.
public ValueTask DisposeAsync()
Returns
ValueTask
ExecuteNonQueryAsync(string, params (string, object)[])
Executes a SQL statement that does not return any result set.
public Task<int> ExecuteNonQueryAsync(string sql, params (string, object)[] parameters)
Parameters
sql string
The SQL statement to execute.
parameters (string, object)[]
Optional parameters to bind to the command.
Returns
Task<int>
The number of rows affected by the command.
ExecuteReaderAsync(string, params (string, object)[])
Executes a SQL query and returns a data reader for reading the result set.
public Task<SqliteDataReader> ExecuteReaderAsync(string sql, params (string, object)[] parameters)
Parameters
sql string
The SQL query to execute.
parameters (string, object)[]
Optional parameters to bind to the command.
Returns
Task<SqliteDataReader>
A
ExecuteScalarAsync(string, params (string, object)[])
Executes a SQL statement and returns the first column of the first row.
public Task<object> ExecuteScalarAsync(string sql, params (string, object)[] parameters)
Parameters
sql string
The SQL statement to execute.
parameters (string, object)[]
Optional parameters to bind to the command.
Returns
Task<object>
The first column of the first row in the result set.
GetCommand(string)
Retrieves a previously registered command by key.
public SqliteCommand GetCommand(string key)
Parameters
key string
The command key.
Returns
SqliteCommand
The associated
Exceptions
KeyNotFoundException
Thrown if no command exists for the specified key.
HasCommand(string)
Determines whether a command with the specified key exists.
public bool HasCommand(string key)
Parameters
key string
The command key.
Returns
bool
true if the command exists; otherwise, false.
OpenConnectionAsync()
Opens the SQLite connection asynchronously if it is not already open.
public Task OpenConnectionAsync()
Returns
Task
Remarks
If the connection has not yet been created, it will be instantiated using the provided connection string.
RemoveCommandAsync(string)
Removes and disposes a previously registered command.
public Task RemoveCommandAsync(string key)
Parameters
key string
The unique key identifying the command to remove.
Returns
Task
RollbackTransactionAsync(SqliteTransaction)
Rolls back a transaction and disposes it.
public Task RollbackTransactionAsync(SqliteTransaction tx)
Parameters
tx SqliteTransaction
The transaction to roll back.
Returns
Task