2

データベーステーブルから単語をロードするシステムを構築しましたが、それらの単語を「Choices」タイプ (Grammar Building に必要なタイプ) のリストに追加する必要があります。

これは、データベースから単語を取得するように要求するための私のコードです。

            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 リストに追加するには、どうすればよいですか?

4

1 に答える 1

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

SqlDataAdapterと の塗りつぶしも削除したことに注意してくださいDataSet。このコンテキストでは役に立たず、2 つのループを実行しているため、明らかにパフォーマンスが低下します。1 つ目はフレームワークによって実行されて DataSet に入力され、2 つ目はコードによってList<string>変数に単語が追加されます。

于 2014-10-03T15:37:28.503 に答える