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?

落爺英雄遲暮 提交于 2020-06-28 05:43:49

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/54824671/can-we-connect-microsoft-azure-bot-to-azure-sql-database-and-read-data-from-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!