简体   繁体   中英

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)
    .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
    .AsSelf()
    .SingleInstance();

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

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

Please make sure to configure Azure SQL Database firewall .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM