DataEngine for .NET Standard | ComponentOne
Work with Data Engine / Connect Data Engine / Connect Data Engine to Database
In This Topic
    Connect Data Engine to Database
    In This Topic

    With the Data Engine library, you can import data from an SQL database to the DataEngine table into a queryable collection of objects in your code.

    Note: For importing data from a SQL Server database file into the DataEngine table, you need to add the ‘System.Data.SqlClient’ NuGet package in your application, apart from the ‘C1.DataEngine.Core’ and ‘C1.DataEngine.Core.Api’ NuGet packages.

    To connect Data Engine to Database, you need to follow the steps given below:

    1. 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");
    2. Initialize the connection string to the database file whose data you wish to import to the DataEngine base tables.

      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);
      }
    3. Create SqlConnection and SqlCommand objects to get hold of 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 initialized above, as parameters to its constructor. Use the DbConnector’s GetData method 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");
    4. After the connection has been established, the queries can be defined and executed to fetch the desired data. Refer to the Transform Data topic for more information.