This section will help you to get started with the DataEngine library:
Create .NET Core application:
- Create a new .Net Core 2.2 console web application.
- Add the C1.DataEngine and C1.DataEngine.Api NuGet packages to your application.
Connect DataEngine to DataSource
- In the Main method of the Program.cs file, add the following code to initialize a new workspace folder relative to the project root directory using the Init method of the Workspace class.
//Initialize a new workspace folder relative to the project root directory
Workspace workspace = new Workspace();
workspace.Init("workspace");
-
Initialize the connection string to the database file, the data of which you wish to import to the DataEngine base tables using the following code:
public string GetConnectionString()
{
string filename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common\NORTHWND.MDF;";
return String.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0}; Integrated Security=True;Connect Timeout=30;User Instance=True", filename);
}
-
Create SqlConnection and SqlCommand objects to hold the desired data that needs to be imported.
SqlConnection conn = new SqlConnection(GetConnectionString());
conn.Open();
var command = new SqlCommand("Select * from Invoices", conn);
-
Create an instance of the DbConnector class and pass the Workspace, SqlConnection and SqlCommand objects as parameters to its constructor. Use the GetData method of DbConnector class to create a DataEngine table containing the imported data.
//Import data from database to a DataEngine table
var connector = new DbConnector(workspace, conn, command);
connector.GetData("Invoices");
Define and Execute Query
-
Once the DataEngine base table is created, retrieve it using the table method of Workspace class. Perform desired queries on the data using the query method of Workspace class.
//Retrieve the base table for use in constructing queries
dynamic invoice = workspace.table("Invoices");
-
Execute the query by invoking the Execute method of the Query class.
//Find the total sales by country
dynamic query = workspace.query("SalesByCountry", new
{
invoice.Country,
Sales = Op.Sum(invoice.ExtendedPrice)
});
query.Query.Execute();
Display Query Results
-
Retrieve the query results using the GetQueryData method of the Workspace class. Finally, print the query results on the Console window using the Write method of the DataList class.
//Output query results in CSV format to the console
IDataList sales = workspace.GetQueryData("SalesByCountry");
Console.WriteLine("Sales by Country:");
DataList.Write(sales, Console.Out);