Skip to content

Data Connections

Gerard Llort edited this page Mar 3, 2016 · 13 revisions

Cosmo Data Services implements an unified data access layer to simplify developing with multiples data sources.

Data Modules

A Data Module is a simple element that implements a unified access to databases, and currently is based on Microsoft OleDb technology that allows to connect to multiple datasources (more information at https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx).

To develop a new module (for example, to use Excel as a datasource) you should create an implementation of abstract class Cosmo.Data.Connection.DataModule.

At this moment Cosmo only implements the SQL Server Data Module: Cosmo.Data.Connection.Impl.SqlServerDataModule.

Configuration

In this section of cosmo.config.xml you define all the connections that the application use:

<!-- DATA SERVICES -->
<data-services default="my-app-db">

  <connection id="my-app-db" driver="Cosmo.Data.Connection.Impl.SqlServerDataModule">
    <param key="db.server" value="SQLEXPRESS" />
    <param key="db.schema" value="APP_DATA" />
    <param key="db.login" value="sa" />
    <param key="db.password" value="xxxxx" />
  </connection>

  <connection id="corp-data" driver="Cosmo.Data.Connection.Impl.SqlServerDataModule">
    <param key="db.server" value="ACME_SQLSRV" />
    <param key="db.schema" value="ACME_DATA" />
    <param key="db.login" value="sa" />
    <param key="db.password" value="xxxxx" />
  </connection>

</data-services>

Using Data Connection

Using the default connection:

// Open connection
Workspace.DataSource.Connect();

// Execute a SQL query
string sql = "SELECT * FROM SALES WHERE  saleid = @saleid";
SqlCommand cmd = new SqlCommand(sql, Workspace.DataSource.Connection);
cmd.Parameters.Add(new SqlParameter("@saleid", saleId));

SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
   (...)
}

// Close connection
Workspace.DataSource.Disconnect();

Using a specific connection is the same but using its id (defined in the configuration):

Workspace.GetDataSource("corp-data").Connect();
Clone this wiki locally