我们能否将Microsoft Azure Bot连接到Azure SQL数据库并从数据库读取数据并将其用作对Bot的答复? [英] 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?

查看:95
本文介绍了我们能否将Microsoft Azure Bot连接到Azure SQL数据库并从数据库读取数据并将其用作对Bot的答复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个ChatBot项目,该项目需要从Azure托管的SQL数据库中的表中查询,并将结果用作对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.

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

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.

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

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

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

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.

先谢谢您了:)

推荐答案

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

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

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();

Azure SQL数据库的连接字符串应该在web.config中:

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>

请确保配置

这篇关于我们能否将Microsoft Azure Bot连接到Azure SQL数据库并从数据库读取数据并将其用作对Bot的答复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆