# Connect Data Engine to Database

An easy tutorial to connect dataengine to database.

## Content

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.

>type=note
> **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](/componentone/api/services/online-dataengine/dotnet-standard-api/C1.DataEngine/C1.DataEngine.Workspace.html) class.

    ```csharp
    //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.

    ```csharp
    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.

    ```csharp
    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.

    ```csharp
    //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](/componentone/docs/services/online-dataengine/workwithdataengine/transformdata) topic for more information.