Can we connect Microsoft Azure Bot to Azure SQL Database and read data from the DB and use it as a reply for the Bot?


I am working on a ChatBot project, which requires to query from a table in SQL database hosted in Azure and use the result as a reply for the bot.

I am using a basic bot template from Azure Web App Bot. Independently without connecting to the database, the Bot is working fine. And there is no issues with the database, I was able to query from the same DB using EF DB first approach in a MVC webapp.

But in the project, if I use the same approach, I am getting an error: connect ECONNREFUSED

And the Bot is not able work or connect in such a case. Can anyone help me resolve this issue, or if I can get a detailed document to develop a bot which can query and interact to Azure SQL database should also work.

Thanks in advance :)


Here you will find a simple example of how to connect to Azure SQL Database from a Bot and insert data.

To use Azure SQL, please configure the Autofac Dependency Injection in Global.asax. Particularly the following is the piece of code that configures injection of Azure Sql Storage:

var store = new SqlBotDataStore(ConfigurationManager.ConnectionStrings["BotDataContextConnectionString"].ConnectionString);
builder.Register(c => store)

The connection string to Azure SQL Database should be in the web.config:

    <add name="BotDataContextConnectionString" 
        connectionString="Server=tcp:[YourDatabaseServerName],1433;Initial Catalog=[YourDatabaseName];Persist Security Info=False;User ID=[YourDatabaseUserId];Password=[YourDatabaseUserPassword];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />

Please make sure to configure Azure SQL Database firewall.

enter image description here