问题
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;
}
}
HOWEVER, This throws an exception: System.FormatException which also states the message:
FormatException was unhandled
Double-quoted string not valid.
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?
回答1:
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;
}
}
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
来源:https://stackoverflow.com/questions/26181706/c-sharp-loading-words-from-database-and-adding-them-to-a-list-of-type-choices