Class SqlStore
Sql store. Used in executing SELECT, INSERT, UPDATE, DELETE, etc commands, using a DbConnection.
Many methods use a Params parameter.
That Params can be:
a. either a comma separated list of parameters or
b. the Params[0] element, that is the first element in Params, may be
- a DataRow
- a generic IDictionary<string, object>
- or an IList or Array
and in this second case no other Params elements are used
public class SqlStore
- Inheritance
-
SqlStore
- Inherited Members
- Extension Methods
Constructors
SqlStore(DbConnectionInfo)
Constructor.
public SqlStore(DbConnectionInfo ConnectionInfo)
Parameters
ConnectionInfoDbConnectionInfo
Properties
ConnectionInfo
The connection info used by this instance.
public DbConnectionInfo ConnectionInfo { get; }
Property Value
Provider
The SqlProvider used by this instance.
public SqlProvider Provider { get; }
Property Value
Methods
BeginTransactionContext()
Creates a transaction context that owns both the connection and the transaction.
public virtual SqlTransactionContext BeginTransactionContext()
Returns
CanConnect(bool)
Returns true if this connection info is valid and can connect to a database.
public bool CanConnect(bool ThrowIfNot = false)
Parameters
ThrowIfNotbool
Returns
CreateTable(string)
Creates a new table in the database by executing CommandText. Returns true only if creates the table, false if the table already exists.
The method creates a table generator too, if the database supports generators/sequences.
CommandText should be a CREATE TABLE statement and can contain datatype placeholders. See ReplaceDataTypePlaceholders(string) for details.
public virtual bool CreateTable(string SqlText)
Parameters
SqlTextstring
Returns
EnsureConnection(string)
Ensures that a connection can be done by opening and closing the connection.
public virtual void EnsureConnection(string ConnectionString)
Parameters
ConnectionStringstring
ExecSql(Action<DbTransaction>)
It can be used in executin multiple SQL operations inside a single transaction using a callback.
Example
Provider.ExecSql(ConnectionString,
tr => Provider.ExecSql(tr, Sql1, Params1),
tr => Provider.ExecSql(tr, Sql2, Params2),
tr => Provider.ExecSql(tr, Sql3, Params3)
);
public void ExecSql(Action<DbTransaction> Action)
Parameters
ActionAction<DbTransaction>
ExecSql(IEnumerable<string>)
Executes a list of executable statements inside a single transaction
public virtual void ExecSql(IEnumerable<string> SqlTextList)
Parameters
SqlTextListIEnumerable<string>
ExecSql(DbTransaction, string)
Executes a single SQL operation inside a transaction.
public int ExecSql(DbTransaction Transaction, string SqlText)
Parameters
TransactionDbTransactionSqlTextstring
Returns
ExecSql(DbTransaction, string, params object[])
Executes a single SQL operation inside a transaction.
public int ExecSql(DbTransaction Transaction, string SqlText, params object[] Params)
Parameters
TransactionDbTransactionSqlTextstringParamsobject[]
Returns
ExecSql(string)
Executes a SQL statement (INSERT, UPDATE, DELETE, κλπ) and returns the number of rows affected.
public int ExecSql(string SqlText)
Parameters
SqlTextstring
Returns
ExecSql(string, params object[])
Executes a SQL statement (INSERT, UPDATE, DELETE, κλπ) and returns the number of rows affected.
public int ExecSql(string SqlText, params object[] Params)
Parameters
Returns
FieldExists(string, string)
Returns true if the FieldName exists in TableName table.
public bool FieldExists(string TableName, string FieldName)
Parameters
Returns
GetFieldNames(string)
Returns a string list with the field names of the TableName
public List<string> GetFieldNames(string TableName)
Parameters
TableNamestring
Returns
GetIndexNames()
Returns a string list with the index names in the database
public List<string> GetIndexNames()
Returns
GetNativeSchemaFromSelect(string, string)
Gets the native schema of the SqlText SELECT statement..
public DataTable GetNativeSchemaFromSelect(string StatementName, string SqlText)
Parameters
Returns
GetNativeSchemaFromTableName(string, string)
Gets the native schema of the TableName table.
public DataTable GetNativeSchemaFromTableName(string StatementName, string TableName)
Parameters
Returns
GetSchema()
Returns schema information for the data source of this System.Data.Common.DbConnection.
public virtual DataTable GetSchema()
Returns
GetSchema(string)
Returns schema information for the data source of this System.Data.Common.DbConnection using the specified string for the schema name.
public virtual DataTable GetSchema(string collectionName)
Parameters
collectionNamestring
Returns
GetSchema(string, string[])
Returns schema information for the data source of this System.Data.Common.DbConnection using the specified string for the schema name and the specified string array for the restriction values.
public virtual DataTable GetSchema(string collectionName, string[] restrictionValues)
Parameters
Returns
GetServerDateTime()
Gets the datetime value from the server.
public DateTime GetServerDateTime()
Returns
GetTableNames()
Returns a string list with the table names in the database.
public List<string> GetTableNames()
Returns
GetViewNames()
Returns a string list with the view names in the database.
public List<string> GetViewNames()
Returns
IndexExists(string)
Returns true if an index with IndexName exists in the database.
public bool IndexExists(string IndexName)
Parameters
IndexNamestring
Returns
IntegerResult(DbTransaction, string, int, params object[])
Ideal for executing SELECT statements of the type "select count(ID) as COUNT_ID from TABLE_NAME where ID = 1234"
public int IntegerResult(DbTransaction Transaction, string SqlText, int Default, params object[] Params)
Parameters
TransactionDbTransactionSqlTextstringDefaultintParamsobject[]
Returns
IntegerResult(string, int)
Ideal for executing SELECT statements of the type "select count(ID) as COUNT_ID from TABLE_NAME where ID = 1234"
public int IntegerResult(string SqlText, int Default)
Parameters
Returns
IntegerResult(string, int, params object[])
Ideal for executing SELECT statements of the type "select count(ID) as COUNT_ID from TABLE_NAME where ID = 1234"
public int IntegerResult(string SqlText, int Default, params object[] Params)
Parameters
Returns
LastId(DbTransaction, string)
Returns the last id produced by an INSERT Sqlt statement.
It should be used only with databases that support identity (auto-increment) columns
public virtual int LastId(DbTransaction Transaction, string TableName)
Parameters
TransactionDbTransactionTableNamestring
Returns
LastId(string)
Returns the last id produced by an INSERT Sqlt statement.
It should be used only with databases that support identity (auto-increment) columns
public virtual int LastId(string TableName)
Parameters
TableNamestring
Returns
NextId(DbTransaction, string)
Returns the next id value of a generator named after the TableName table.
It should be used only with databases that support generators or when a CustomOid object is used.
public virtual int NextId(DbTransaction Transaction, string TableName)
Parameters
TransactionDbTransactionTableNamestring
Returns
NextId(string)
Returns the next id value of a generator named after the TableName table.
It should be used only with databases that support generators or when a CustomOid object is used.
public virtual int NextId(string TableName)
Parameters
TableNamestring
Returns
NextIdByGenerator(DbTransaction, string)
Returns the next value of the GeneratorName generator.
public virtual int NextIdByGenerator(DbTransaction Transaction, string GeneratorName)
Parameters
TransactionDbTransactionGeneratorNamestring
Returns
NextIdByGenerator(string)
Returns the next value of the GeneratorName generator.
public virtual int NextIdByGenerator(string GeneratorName)
Parameters
GeneratorNamestring
Returns
OpenConnection()
Creates and opens a DbConnection
public DbConnection OpenConnection()
Returns
ResetTable(string)
Empties the TableName table in the database and initializes its generator/sequencer or identity column.
DANGEROUS
public void ResetTable(string TableName)
Parameters
TableNamestring
Select(DbTransaction, string, params object[])
Executes a SELECT statement inside a transaction and returns the result as a DataTable.
public MemTable Select(DbTransaction Transaction, string SqlText, params object[] Params)
Parameters
TransactionDbTransactionSqlTextstringParamsobject[]
Returns
Select(string)
Executes a SELECT statement and returns the result as a DataTable.
public MemTable Select(string SqlText)
Parameters
SqlTextstring
Returns
Select(string, params object[])
Executes a SELECT statement and returns the result as a DataTable.
public MemTable Select(string SqlText, params object[] Params)
Parameters
Returns
SelectIncrementLocked(string, string, object, string)
Selects a row with update lock, increments an integer field, and returns the initial row.
Example
DataRow Row = SelectIncrementLocked("NumberSeries", "Code", "SALES_DOMESTIC_1", "NextNumber");
public virtual DataRow SelectIncrementLocked(string TableName, string KeyFieldName, object KeyValue, string ValueFieldName)
Parameters
Returns
SelectResult(DbTransaction, string, object, params object[])
Ideal for executing SELECT statements of the type "select FIELD_NAME from TABLE_NAME"
public object SelectResult(DbTransaction Transaction, string SqlText, object Default, params object[] Params)
Parameters
TransactionDbTransactionSqlTextstringDefaultobjectParamsobject[]
Returns
SelectResult(string)
Ideal for executing SELECT statements of the type "select FIELD_NAME from TABLE_NAME"
public object SelectResult(string SqlText)
Parameters
SqlTextstring
Returns
SelectResult(string, object)
Ideal for executing SELECT statements of the type "select FIELD_NAME from TABLE_NAME"
public object SelectResult(string SqlText, object Default)
Parameters
Returns
SelectResult(string, object, params object[])
Ideal for executing SELECT statements of the type "select FIELD_NAME from TABLE_NAME"
public object SelectResult(string SqlText, object Default, params object[] Params)
Parameters
Returns
SelectResults(DbTransaction, string, params object[])
Executes SqlText and returns the first DataRow of the result set.
WARNING: If SqlText returns no rows at all then this method returns null.
Params can be:
1. either a comma separated C# params list
2. or the Params[0] element, that is the first element in Params, may be a DataRow, generic IDictionary, IList or Array and in that case no other Params elements are used.
public DataRow SelectResults(DbTransaction Transaction, string SqlText, params object[] Params)
Parameters
TransactionDbTransactionSqlTextstringParamsobject[]
Returns
SelectResults(string)
Executes SqlText and returns the first DataRow of the result set.
WARNING: If SqlText returns no rows at all then this method returns null.
public DataRow SelectResults(string SqlText)
Parameters
SqlTextstring
Returns
SelectResults(string, params object[])
Executes SqlText and returns the first DataRow of the result set.
WARNING: If SqlText returns no rows at all then this method returns null.
Params can be:
1. either a comma separated C# params list
2. or the Params[0] element, that is the first element in Params, may be a DataRow, generic IDictionary, IList or Array and in that case no other Params elements are used.
public DataRow SelectResults(string SqlText, params object[] Params)
Parameters
Returns
SelectTo(DbTransaction, MemTable, string, params object[])
Executes a SELECT statement and loads the result into the specified DataTable using a transaction.
public int SelectTo(DbTransaction Transaction, MemTable Table, string SqlText, params object[] Params)
Parameters
TransactionDbTransactionTableMemTableSqlTextstringParamsobject[]
Returns
SelectTo(MemTable, string)
Executes a SELECT statement and loads the result into the specified DataTable.
public int SelectTo(MemTable Table, string SqlText)
Parameters
Returns
SelectTo(MemTable, string, params object[])
Executes a SELECT statement and loads the result into the specified DataTable.
public int SelectTo(MemTable Table, string SqlText, params object[] Params)
Parameters
Returns
TableExists(string, bool)
Returns true if a table with TableName exists in the database.
public bool TableExists(string TableName, bool UseSelect = false)
Parameters
Returns
TableIsEmpty(string)
Returns true if a table contains no rows in the database.
public bool TableIsEmpty(string TableName)
Parameters
TableNamestring