我建立了一个从数据库表中加载单词的系统,但我需要将这些单词添加到类型“选择”(语法构建所需的类型)列表中。
这是我请求从数据库中检索单词的代码:
List<string> newWords = new List<string>();
newWords = LexicalOperations.WordLibrary().ToList();
Choices dbList = new Choices(); //Adding them to a Choice List
if (newWords.Count != 0)
{
foreach (string word in newWords)
{
dbList.Add(word.ToString());
}
}
else dbList.Add("Default");
这是我从表中检索数据的代码:
public class LexicalOperations
{
public static List<string> WordLibrary()
{
List<string> WordLibrary = new List<string>();
string conString = "Data Source=.;Initial Catalog=QABase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(conString))
{
connection.Open();
string sqlIns = "select WordList from NewWords";
SqlCommand cmd = new SqlCommand(sqlIns, connection);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
WordLibrary.Add(dr[0].ToString());
}
}
return WordLibrary;
}
}
但是,这会引发异常:System.FormatException,它还说明了消息:
FormatException 未处理
双引号字符串无效。
当我在 Speech Grammar Builder 中构建选择列表时会引发此错误:
GrammarBuilder graBui = new GrammarBuilder(dbList);
Grammar Gra = new Grammar(graBui);
我究竟做错了什么?为了从数据库中正确检索单词并将它们添加到选择列表中,应该做什么?