【发布时间】:2020-01-08 04:46:44
【问题描述】:
我正在开发一个 MS Bot Framework 项目,我正在从 C# 数据库中的键值对中检索值。以前我有这个:
var list = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(obj,_Obj.Questions)
};
Dictionary<int, string> d = new Dictionary<int, string>
{
{ 1, "Welcome, How are you?" },
{ 2, "Tell me something about yourself."},
{ 3, "How much experience do you have?"},
};
我的目标是从数据库中获取诸如“欢迎光临,你好吗?”、“告诉我一些关于你自己的事情”等价值观。为了实现这一点,我这样做了:
编辑:
Questions.cs
public static string GetChats(int Id)
{
using (SqlCommand cmd = new SqlCommand("usp_FetchData", con))
{
var list = new List<KeyValuePair<int, string>>();
DataTable dt = new DataTable();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", Id);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
foreach (DataRow row in dt.Rows)
{
string queMsg = row["Description"]?.ToString();
list.Add(new KeyValuePair<int, string>(Id, queMsg));
}
}
// class property
public string WelcomeStmt = GetChats(1).ToString();
上面函数的值是在这个方法中获取的:
MyDialog.cs // 这是 Bot 的对话框
private static async Task<DialogTurnResult> **NameStepAsync**(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text(questions.AskName) }, cancellationToken);
}
我在 GetChats(Id) 方法中将 Id 值作为 1 传递。所以基于此,我应该得到对应的Value。
在 NameStepAsync 方法中,我收到了一个我猜想的不寻常的父类,而不是我期望的实际字符串。:
System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]。
有人知道为什么会这样吗?
谢谢。
【问题讨论】:
-
@Jawad,谢谢这有点帮助。我在 foreach 循环中使用了 dt.Rows。但是,在调试模式下,我得到的是上面的父类而不是实际的字符串。
-
您在问题中输入的代码似乎并未向我们显示字符串
System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]的来源。看来您实际上是在.ToString()实例上执行.ToString()。但我在代码中看不到这一点。您需要提供minimal reproducible example。 -
@Enigmativity 请查看我更新的问题。
-
@RohanRao - 您还没有提供代码的minimal reproducible example。例如,
GetChats方法的结尾缺失。
标签: c# sql-server botframework keyvaluepair