Depending on our application need and location of the
database (local or remote), the connections may vary. It can be either done
through a DSN or we can use a connection string to connect to the database.
Connection String
For direct connection strings the Keywords play a major role
and are case sensitive. The Connection String Keywords available in DBISAM
ODBC Driver are as follows:
·
DRIVER: It specifies the ODBC driver
name used.
·
ConnectionType: Depends on the
connection, i.e. Local or Remote.
·
CatalogName: Specifies the name of the
database.
These were the required Keywords for both Local and Remote
Connections. The following keywords are required only for Remote connections.
·
UID: Specifies the User ID for the
remote connection.
·
PWD: Specifies the password for the
remote connection.
·
RemoteHostName: Specifies the Host name
of the remote Database server.
·
RemoteIPAddress: Specifies the IP
address of the remote Database Server.
From the RemoteHostName or RemoteIPAddress above, any one is
used.
Examples
The different connection types that can be used are listed
below.
1.
Connection through a preconfigured DSN
Listing 2
OdbcConnection objODBCCon = new OdbcConnection(“DSN=?”);
//? : Your preconfigured DSN
2.
Connection to the Database present on a local system through a
connection string.
Listing 3
string txtConStr = “DRIVER={DBISAM 4 ODBC Driver}”;
txtConStr += “ConnectionType=Local;CatalogName=path”;
//path : The complete path to the folder, where the DBISAM Tables
//(i.e. *.dat files) are present.
OdbcConnection objODBCCon = new OdbcConnection(txtConStr);
3.
Connection to the Database present on a remote system through a
connection string.
Listing 4
string txtConStr = “DRIVER={DBISAM 4 ODBC Driver}”;
txtConStr += “ConnectionType=Remote;CatalogName=employee;”;
//employee : The database present on the Remote Server.
txtConStr += “UID=userid;PWD=password;”;
txtConStr += “RemoteIPAddress=xxx.xxx.xxx.xxx”;
//xxx.xxx.xxx.xxx : The IP Address of the Remote Server.
OdbcConnection objODBCCon = new OdbcConnection(txtConStr);
After establishing this connection we can access the
database and the tables through a DataSet or DataReader as we usually do in any
other ODBC Connections. The following code shows a way to get all the Table
names from a specified folder (dbisamFolder) and
display it in a list box (lstAvailable).
Listing 5
string table;
objODBCCon.Open();
string[] files;
// Gets all the DBISAM Tables from the user Specified folder
files = Directory.GetFiles(dbisamFolder.ToString(),"*.dat");
// Extracts the DBISAM table names from the .dat files
// and excludes the tables.
for(int Count = 0; Count < files.Length; Count++)
{
table = files[Count].Remove(files[Count].LastIndexOf("."), 4);
table = table.Substring(table.LastIndexOf("\\") + 1);
lstAvailable.Items.Add(table);
// lstAvailable is a ListBox
}
Now the table names can be used to capture the table into a
DataSet and do the required operations.
Listing 6
table = lstAvailable.Items[0].ToString();
OdbcDataAdapter objDA;
DataSet objDS = new DataSet("DbiSam");
objODBCCon.Open();
objDA = new OdbcDataAdapter("SELECT * FROM " + table, objODBCCon);
objDA.Fill(objDS);