我们能否将Microsoft Azure Bot连接到Azure SQL数据库并从数据库读取数据并将其用作对Bot的答复?

时间:2019-02-22 10:08:14

标签: c# azure azure-sql-database botframework azure-bot-service

我正在研究一个ChatBot项目,该项目需要从Azure托管的SQL数据库中的表中查询,并将结果用作对机器人的答复。

我正在使用Azure Web App Bot中的基本bot模板。 Bot无需连接数据库就可以独立运行。数据库没有问题,我能够在MVC Web应用程序中使用EF DB first方法从同一数据库中查询。

但是在项目中,如果我使用相同的方法,则会出现错误: 连接ECONNREFUSED

在这种情况下,机器人无法工作或无法连接。 任何人都可以帮助我解决此问题,或者是否可以获取详细的文档来开发可以查询并与Azure SQL数据库进行交互的漫游器。

预先感谢:)

1 个答案:

答案 0 :(得分:1)

Here,您将找到一个简单的示例,该示例说明如何从Bot连接到Azure SQL数据库并插入数据。

要使用Azure SQL,请在Global.asax中配置Autofac依赖项注入。特别是以下是配置Azure Sql Storage注入的代码段:

var store = new SqlBotDataStore(ConfigurationManager.ConnectionStrings["BotDataContextConnectionString"].ConnectionString);
builder.Register(c => store)
    .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
    .AsSelf()
    .SingleInstance();

Azure SQL数据库的连接字符串应该在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>

请确保配置Azure SQL Database firewall

enter image description here