Class PolarConnection
- Namespace
- PolarDataInterop
- Assembly
- PolarDataInterop.dll
This class is used to communicate with the database.
public class PolarConnection : IDisposable
- Inheritance
-
PolarConnection
- Implements
- Inherited Members
- Extension Methods
Constructors
PolarConnection(string)
Constructor using a specific connection string.
public PolarConnection(string pConnectionString = "")
Parameters
pConnectionString
stringThe connection string to use.
Fields
OnError
Weak event interface.
public PolarWeakEvent OnError
Field Value
Properties
AutoCloseConnection
If this property is set to True, the connection is automatically closed as soon as no transactions or recordsets are open. Can be set to false, to keep for example a 'USE' statetment working.
public bool AutoCloseConnection { get; set; }
Property Value
CommandTimeout
Gets/sets the timeout for commands in seconds.
public int CommandTimeout { get; set; }
Property Value
Connection
Gets the underlying connection.
public DbConnection Connection { get; }
Property Value
ConnectionString
Returns the underlying connection string.
public string ConnectionString { get; }
Property Value
ConnectionTimeout
Gets the timeout for connecting to the database. Read only.
public int ConnectionTimeout { get; set; }
Property Value
InTransaction
Returns True if a transaction is open on this connection.
public bool InTransaction { get; }
Property Value
ServerType
Returns the type of server.
public ServerType ServerType { get; }
Property Value
Methods
BeginTransaction()
To start a transaction. Only one transaction can be in place.
public bool BeginTransaction()
Returns
- bool
A boolean indicating success.
Exceptions
- Exception
Throws an error if already in a transaction or if the BEGIN TRANSACTION failed.
Clone()
Clones the connection
public PolarConnection Clone()
Returns
- PolarConnection
A cloned connection.
Close()
Closes the connection and frees resources.
public void Close()
CloseOpenRecordsets()
This method closes all still open recordsets. Only workd for SQL Server connections.
public void CloseOpenRecordsets()
CommitTransaction()
Commits the open transaction.
public void CommitTransaction()
Exceptions
- Exception
Raises an error if not in a transaction.
Dispose()
Disposal.
public void Dispose()
EasyConnect_Jet(string, string, string, string)
Helper to easily connect to a Microsoft Jet database.
public PolarConnection EasyConnect_Jet(string pDatabasePath, string pMDWpath = "", string pUserName = "", string pPassword = "")
Parameters
pDatabasePath
stringThe name of the database file to open.
pMDWpath
stringThe name of the security file (MDW file) to use. Leave empty if not using a security file.
pUserName
stringThe user name to connect with.
pPassword
stringThe password to connect with.
Returns
EasyConnect_SqlServer(string, string, string, string)
Helper to easily connect to a SQL server database.
public PolarConnection EasyConnect_SqlServer(string pServer, string pDataBase, string pUserName = "", string pPassword = "")
Parameters
pServer
stringThe name of the server to connect to.
pDataBase
stringThe name of the database to connect to.
pUserName
stringThe username to connect with. Use an empty string to integrated authentication.
pPassword
stringThe password to connect with. Use an empty string to integrated authentication.
Returns
- PolarConnection
This Polar Connection object.
Execute(string, bool)
This method executes a SQL statetment on the current database.
public void Execute(string pSQL, bool pSuppressNewConnection = false)
Parameters
pSQL
stringThe SQL statement to execute.
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Execute(string, ref int, bool)
This method executes a SQL statetment on the current database.
public void Execute(string pSQL, ref int pRecordsAffected, bool pSuppressNewConnection = false)
Parameters
pSQL
stringThe SQL statement to execute.
pRecordsAffected
intThis by-ref int parameter will contain the numer of records affected.
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
ExecuteNonQuery(string, bool)
This method executes a SQL statetment on the current database and returns the number of records affected.
public int ExecuteNonQuery(string pSQL, bool pSuppressNewConnection = false)
Parameters
pSQL
stringThe SQL statement to execute.
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- int
The number of affected records.
ExecuteScalar(string, bool)
This method executes a SQL statetment on the current database and returns the value returned by the database.
public object ExecuteScalar(string pSQL, bool pSuppressNewConnection = false)
Parameters
pSQL
stringThe SQL statement to execute.
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- object
The returned value.
~PolarConnection()
Disposal
protected ~PolarConnection()
GetCatalog(string, string)
Gets the catalog used to get information about the tables and views in this database.
public bpxCatalog GetCatalog(string pCatalogName = "", string pSchemaName = "dbo")
Parameters
pCatalogName
stringThe name of the catalog to retrieve.
pSchemaName
stringThe schema (like 'dbo') to retrieve.
Returns
Lookup(string, bool)
This method will execute the given query and return the first field of the first record. If no data is found or an exception was raised, NULL will be returned.
public object Lookup(string pQuery, bool pSuppressNewConnection = false)
Parameters
pQuery
stringThe query to execute, like 'SELECT COUNT(*) FROM Customer'
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- object
The value of the first field of the first record returned by the query or NULL in all other cases.
Lookup(string, ref int, ref string, bool)
This method will execute the given query and return the first field of the first record. If no data is found or an exception was raised, NULL will be returned.
public object Lookup(string pQuery, ref int pErrorNumber, ref string pErrorMessage, bool pSuppressNewConnection = false)
Parameters
pQuery
stringThe query to execute, like 'SELECT COUNT(*) FROM Customer'
pErrorNumber
intThis by-ref int parameter will be set to the error number if applicable
pErrorMessage
stringThis by-ref string parameter will be set to the error message if applicable
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- object
The value of the first field of the first record returned by the query or NULL in all other cases.
Open(string)
Sets the connection string to the specified pConnectionString
public void Open(string pConnectionString)
Parameters
pConnectionString
stringThe connection string to use.
OpenDynamicRecordSet(string)
Opens a dynamic recordset that is normally updatable.
public Recordset OpenDynamicRecordSet(string pQuery)
Parameters
pQuery
stringThe query to use for this recordset.
Returns
- Recordset
The dynamic recordset.
OpenStaticRecordSet(string, bool, bool)
Opens a static RecordSet
public Recordset OpenStaticRecordSet(string pQuery, bool pUpdatable = false, bool pSuppressNewConnection = false)
Parameters
pQuery
stringThe query to use for the recordset.
pUpdatable
boolMark the recordset as 'updatable'. This only works for plain queries that include the primary key of the single table that is queried.
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- Recordset
The new static recordset.
RecordExists(string, bool)
This method runs a query and returns True if at least one record was returned. If an exception was raised, this method returns False.
public bool RecordExists(string pQuery, bool pSuppressNewConnection = false)
Parameters
pQuery
stringThe query to execute, like 'SELECT TOP 1 * FROM Customer WHERE Status = 9'
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- bool
True if the given query returned at least one record.
RecordExists(string, ref int, ref string, bool)
This method runs a query and returns True if at least one record was returned. If an exception was raised, this method returns False.
public bool RecordExists(string pQuery, ref int pErrorNumber, ref string pErrorMessage, bool pSuppressNewConnection = false)
Parameters
pQuery
stringThe query to execute, like 'SELECT TOP 1 * FROM Customer WHERE Status = 9'
pErrorNumber
intThis by-ref int parameter will be set to the error number if applicable
pErrorMessage
stringThis by-ref string parameter will be set to the error message if applicable
pSuppressNewConnection
boolIf not in a transaction and AutoCloseConnection = False, then the recordset will be opened in a clone of this connection, unless this parameter is passed as True.
Returns
- bool
True if the given query returned at least one record.
RollBackTransaction()
Rolls back the open transaction.
public void RollBackTransaction()
Exceptions
- Exception
Raises an error if not in a transaction.
ToString()
Returns the connection string.
public override string ToString()
Returns
ValueToSQL(object)
This method converts a value to a SQL string.
public string ValueToSQL(object pValue)
Parameters
pValue
objectThe value to convert.
Returns
- string
A SQL expression representing the value.