Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C从数据库中加载单词并将它们添加到类型“选项”列表中?_C#_Database_Double Quotes_Formatexception - Fatal编程技术网

C# C从数据库中加载单词并将它们添加到类型“选项”列表中?

C# C从数据库中加载单词并将它们添加到类型“选项”列表中?,c#,database,double-quotes,formatexception,C#,Database,Double Quotes,Formatexception,我构建了一个从数据库表加载单词的系统,但我需要将这些单词添加到类型选择列表中,即语法构建所需的类型 这是我请求从数据库检索单词的代码: List<string> newWords = new List<string>(); newWords = LexicalOperations.WordLibrary().ToList(); Choices dbList = new Choices(); //Ad

我构建了一个从数据库表加载单词的系统,但我需要将这些单词添加到类型选择列表中,即语法构建所需的类型

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

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

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

注意,我还删除了SqlDataAdapter和数据集的填充。在这种情况下,它是无用的,并且显然会妨碍性能,因为您正在执行两个循环。第一个由框架执行以填充数据集,第二个由您的代码将单词添加到列表变量中

哪一行引发错误?双引号字符串无效听起来像是自定义异常,可能是由dbList引发的。add引发的?-错误在哪里?当前字符串是什么样子?与问题无关,但前两行可以写成List newWords=LexicalOperations.WordLibrary@亚历克斯克。当我在一个语法构建器中构建选项列表时,会抛出这个错误。它表示您的单词列表包含一个带双引号的字符串。如果无法更改语法类的行为,则需要在从数据库加载字符串时删除这些双引号。这类字符串的示例可能有助于编写正确的答案
GrammarBuilder graBui = new GrammarBuilder(dbList);
Grammar Gra = new Grammar(graBui);
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;
    }
}