What is the best way to access external DB in API?

brandon rogers asked on December 15, 2017 21:19

I need to import an external db's data into a custom table. I am not sure the best way to handle this through the API.

thanks.

Correct Answer

Brenden Kehren answered on December 19, 2017 15:57

Typically what we do is add the connection string in the web.config OR add it as a constant string. Then load the connection via the API and make calls as needed. Something like this:

GeneralConnection conn = ConnectionHelper.GetConnection(SettingsKeyInfoProvider.GetStringValue(SiteContext.CurrentSiteName + ".SettingsKeyCodeName"));

using (GeneralConnection cn = conn)
    {
        try
        {
            cn.Open();
            DataSet ds= cn.ExecuteQuery("query.name.from.custompage.type");
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                // do some work here
            }
        }
        catch {}
    }

This shows you storing your connection string in a settings key within Kentico. You can also store it in the web.config and get it from there too.

Another thing it shows is storing your queries or stored procedures within a custom page type that is only a placeholder for transformations and queries. This will allow you to take advantage of the virtual objects without needing to update your custom code all the time.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Ilesh Mistry answered on December 15, 2017 23:34 (last edited on December 16, 2017 09:55)

Hello

Do you have that external DB's data extracted or have a way to extract it into a format like CSV?

Then this is one way that can help you for the data transfer into a custom table - Kentico import toolkit

1 votesVote for this answer Mark as a Correct answer

Alex Golebiewski answered on December 17, 2017 18:10

Does the import need to be done only once or does it need to stay sync'd? If sync'd are the databases on the same server or different SQL servers?

0 votesVote for this answer Mark as a Correct answer

brandon rogers answered on December 18, 2017 14:08

Thank you for the responses, the database needs to be synced everyday so unfortunately I can't use the the import tool.

My thought process is to create a custom task that will import new items into the custom table from the external database once a day. I would've used the sql datasource but I need to be able to apply the smart search over the data.

I am not sure how to use the MemoryDataQuerySource api or if I should use the data engine to build my connection string so that I can access my external non kentico db. Thank you!

0 votesVote for this answer Mark as a Correct answer

Alex Golebiewski answered on December 19, 2017 03:17

You could use Entity Framework as a connection to your external database and just read from it as if it was part of the Kentico db instead of syncing it daily. This would allow you always pull data as needed and when running your custom search index task, you could pull the data in that is needed for the custom search as well. Entity Framework would also allow you to use LINQ instead of the outdated SQLCommand.

0 votesVote for this answer Mark as a Correct answer

brandon rogers answered on December 19, 2017 16:02

Thank you everyone, this has been very helpful!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.