C#从数据库加载单词并将其添加到“选择"类型的列表中吗? [英] C# Loading words from Database and adding them to a list of type "Choices"?

查看:102
本文介绍了C#从数据库加载单词并将其添加到“选择"类型的列表中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内置的系统,该系统可以从数据库表中加载单词,但是我需要将这些单词添加到类型为选择"(语法构建所需的类型)的列表中.

I have a built a system which loads words from a database table but I need to add those words to a list of type "Choices" (the type that is needed for Grammar Building).

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

This is my code for requesting words to be retrieved from the database:

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

这是我从表中检索数据的代码:

This is my code of retrieving data from the table:

 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 ,该异常还会指出该消息:

HOWEVER, This throws an exception: System.FormatException which also states the message:

FormatException未处理

FormatException was unhandled

双引号字符串无效.

当我在语音语法生成器中构建选择列表时,抛出此错误:

This error is thrown when I build the choices list in a Speech Grammar Builder:

GrammarBuilder graBui = new GrammarBuilder(dbList);
Grammar Gra = new Grammar(graBui);

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

What am I doing wrong? What should be done in order to properly retrieve words from the database and add them to a Choice list?

推荐答案

问题似乎是您的语法类无法处理带有双引号的字符串.
因此,解决问题的最简单方法是删除输入中的双引号.

The problem seems to be that your Grammar class cannot handle strings with double quotes.
So, the simplest way to remove the problem is to remove the double quotes by your input.

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的填充内容.在这种情况下,这是没有用的,并且显然会影响性能,因为您正在执行两个循环.第一个由框架执行以填充数据集,第二个由您的代码将单词添加到List<string>变量

Notice that I have also removed the SqlDataAdapter and the filling of a DataSet. In this context is useless and clearly hinders the performance because you are executing two loops. The first one is executed by the Framework to fill the DataSet, the second by your code to add the words to the List<string> variable

这篇关于C#从数据库加载单词并将其添加到“选择"类型的列表中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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