How to use DataContext?
Datacontext supplies the class CDataContext. That is used in applications that make connection to a database, and in which you want to be able to switch between databases for different circumstances like testing, developing, useing production data etc.
CDataContext knows the syntax that is used to store the settings in the config file, so setting and reading is done with CDataContext.
Also the choice of which setting to use is kept by CDataContext (with an id in the configuration file) and the contexts can be given a name; a list of them can be asked from CDataContext.
Create a new context with newDataContext(
name you want to give, Optional description, optional id of choice)
gives back the id that is created for it.
You can then simply use an existing Connection object (eg hConData) that you filled with the right data to connect to the database, and make CDataContext save that to the configuration file with saveConnection(idOfContextItBelongsTo, datasetString it is saved under, Connection object); DataContexts count from zero.
Eg: hDataContext.saveConnection(0, "DbEmployees", hConData)
To connect afterwards:
from application
Define a constant string that is used in the configuration file, Private Const csMyAppDataset As String = "DbMyApp"
(eg "DbEmployees")
Define a connection objecthConData as New Connection
.
Make an object from the DataContext class to manage the settings; hDataContext As New CDataContext
use it like this: hDataContext.makeConnection(hCondata, csMyAppDataset)
with libraries
Suppose you have an application "myApplication".
In that application you use following libraries: MyLibrary, DataContext
MyLibrary has:
- public constant csDATASET
: a string used to identify settings in the config file.
- public hConData
: a Connection object used to connect to the database.
DataContext knows how to store settings in the config file, and how to get them for use.
In your application:
Define hDataContext as New CDataContext
Use like:
hDataContext.MakeConnection(MyLibrary.hCondata, MyLibrary.csDataSet)