如果我输入搜索参数,如何让机器人框架机器人发回数据库的单元格?

How do I get a bot framework bot to send back a cell of a database if i type in a search parameter?

我想在microsoft bot framework中做一个bot,c#,如果你发名字,就发回一个人喜欢的颜色值。数据存储在天蓝色的数据库中。连接很好,问题是当我尝试实现任何 reader 对象时,它会给出代码 500 错误。

示例:

用户:账单

bot: bill 最喜欢的颜色是红色!你想知道其他人最喜欢的颜色吗?

逻辑因此应该获取用户输入的 bill,将其用作 sql 语句中的输入参数,然后读取 return 值并通过机器人返回给用户.

我的 MessagesController.cs class 与机器人模板没有变化,我试图将所有逻辑放在下面的 RootDialog.cs 中:

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Data.SqlClient;

namespace ColourBot.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{

    public Task StartAsync(IDialogContext context)
    {

        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;
        string result10; 



        var cb = new SqlConnectionStringBuilder();
        cb.DataSource = "my_server.database.windows.net";
        cb.UserID = "user_id";
        cb.Password = "pass_word";
        cb.InitialCatalog = "ColourDB";

        using (var connection = new SqlConnection(cb.ConnectionString))
        {
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT Colour FROM People Where Name=ed", connection);

            result10 = Submit_1_Tsql_SelectEmployees(connection);

            await context.PostAsync(

            $"The name you sent was {activity.Text}, their favourite colour is {result10}!");

            context.Wait(MessageReceivedAsync);

            string Build_1_Tsql_SelectEmployees()
            {
                return $@"SELECT
                                 Colour
                          FROM
                                 People
                          Where
                                  Name={activity.Text};";
            }
            string Submit_1_Tsql_SelectEmployees(SqlConnection conn)
            {
                string tsql = Build_1_Tsql_SelectEmployees();
                string nameString = "";

                using (var command = new SqlCommand(tsql, conn))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var nameReturned = reader.GetName(0);
                            nameString = nameReturned.ToString();

                        }
                    }
                }
                return nameString;
            }
        }
    }
}

}

我创建了一个机器人应用程序来测试您提供的代码并使用 Bot Framework Emulator 调试机器人应用程序,我发现异常是由值 Name={activity.Text} 周围缺少单引号引起的.

您可以修改查询字符串以添加单引号:

string Build_1_Tsql_SelectEmployees()
{
    return $@"SELECT Colour FROM People Where [Name]='{activity.Text}';";
}

此外,为了显示用户最喜欢的颜色,您可以使用以下代码return将调用reader.GetName(0)的数据替换为return列名。

using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        //var nameReturned = reader.GetName(0);
        //nameString = nameReturned.ToString();
        nameString = reader[0].ToString();
    }
}

测试结果: