C#从数据库加载单词并将其添加到类型列表" Choices"?

时间:2014-10-03 15:11:49

标签: c# database double-quotes formatexception

我有一个构建的系统,可以从数据库表中加载单词,但是我需要将这些单词添加到“Choices”类型的列表中(语法构建所需的类型)。

这是我的代码,用于请求从数据库中检索单词:

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

我做错了什么?为了从数据库中正确检索单词并将其添加到Choice列表,应该怎么做?

1 个答案:

答案 0 :(得分:2)

问题似乎是你的语法类无法用双引号处理字符串 因此,解决问题的最简单方法是删除输入的双引号。

public class LexicalOperations
{
    public static List<string> WordLibrary()
    {
        List<string> WordLibrary = new List<string>();
        string conString = "Data Source=.;Initial Catalog=QABase;Integrated Security=True";

        string sqlIns = "select WordList from NewWords";
        using (SqlConnection connection = new SqlConnection(conString))
        using (SqlCommand cmd = new SqlCommand(sqlIns, connection))
        {
            connection.Open();
            using(SqlDataReader reader = cmd.ExecuteReader())
            {
                while(reader.Read())
                {
                    string noQuotes = reader.GetString(0).Replace("\"", "");
                    WordLibrary.Add(noQuotes);
                    // In alternative you could also opt to not add a string with double quotes
                    // string noQuotes = reader.GetString(0);
                    // if(noQuotes.IndexOf("\"") < 0)
                    //    WordLibrary.Add(noQuotes);
                }
            }
        }
        return WordLibrary;
    }
}

请注意,我还删除了SqlDataAdapterDataSet的填充。在这种情况下,由于您正在执行两个循环,因此无用且显然会影响性能。第一个由Framework执行以填充DataSet,第二个由您的代码执行以将单词添加到List<string>变量