Skip to main content

Running DataSource by passing the result to the Dictionary via DataSourceApi

Libraries That Should Be Used

The following two libraries should be added to the project.

using Bimser.CSP.DataSource.Api;

using ```<```ProjeAdı```>```. DataSources;

Use Case

In this example, after running the DataSource, the result from the query is populated into a List of type Dictionary<string, object>. Thus, results can be obtained in different data types (objects).

Step 1: Defining the DataSource

As an example, a DataSource is defined in the project with the name TestDataSource, which contains the following query.

QUERY : SELECT ID, USERNAME, FIRSTNAME, LASTNAME FROM OSUSERS

Step 2: Preparing the Codes to Run the DataSource

A DataSource can be run by giving the parameters detailed below to the method named GetData of the DataSourceApi class.

IMPORTANT NOTE: The DataSource to be run must be located in the project where this example is prepared. In this scenario, the DataSource in another external project cannot be run.

In the following code block, it is explained in parts which part should be written and how.

Sample Code:

List```<```Dictionary```<```string, object```>``````>``` allUsers = DataSourceApi.GetData```<```Dictionary```<```string, object```>```, TestDatasourceRequest```>```(

"TestDataSource",

new TestDatasourceRequest()

{

PARAMETRE1 = "Value1",

PARAMETRE2 = "Value2"
},

Context

);
  • DataSourceApi.GetData<Dictionary<string, object>, TestDatasourceRequest>( The part that says "Dictionary<string, object>" is given because we will get the return in this way. For the "TestDatasourceRequest" part, up to "Request" is given the name of the DataSource to be run. That is, the name of the DataSource and the word Request are written contiguously.

  • "TestDataSource" : The name of the DataSource to run is given.

  • new TestDatasourceRequest() ... : Given the parameters to send to the query. If the parameter is not to be given, it should be left as follows. new TestDatasourceRequest()

  • Context: Context information is given here. It can be left as it is.

Reading the result from the list

The following example illustrates how to read the result from the query. Different methods can also be used.

if (allUsers.Count ```>``` 0 ) // results are coming.

{

As an example, the first line from List will be read.

Dictionary```<```string, object```>``` firstItem = allUsers[0];

int userID = Convert.ToInt32(firstItem["ID"]);

string userName = firstItem["USERNAME"]. ToString();

string firstName = firstItem["FIRSTNAME"]. ToString();

string lastName = firstItem["LASTNAME"]. ToString();

}