我要编写一个程序来查找段落的分数。数据库文件有一个单词分数。
数据库文件
Word Pos_Score
intelligent .987
allows .378
agree .546
industries .289
guests .874
使用 SELECT 查询和 WHERE 类,我将段落单词与数据库文件进行了比较。
段落:
I agree with you. It seems an intelligent tourist industry allows its guests to either immerse fully, in part, or not, depending upon the guest.
上面的段落有一些与数据库文件单词匹配的单词,因此将提取该单词的分数。
程序输出应该是这样的
Pos_score= .987+.378+.546+.289+.874=3.074
代码
private void button1_Click(object sender, EventArgs e)
{
string MyConString = "server=localhost;" +"database=sentiwornet;" + "password=zia;" +"User Id=root;";
StreamReader reader = new StreamReader("D:\\input.txt");
float amd=0;
string line;
float pos1 = 0;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(' ');
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
foreach (string part in parts)
{
command.CommandText = "SELECT Pos_Score FROM score WHERE Word = part";
try
{
amd = (float)command.ExecuteScalar();
pos1 = amd + pos1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Reader = command.ExecuteReader();
connection.Open();
}
}
MessageBox.Show("The Positive score of Sentence is="+pos1.ToString());
reader.Close();
}
但这段代码不起作用,它给出了以下错误。
错误 1
对象引用未设置为对象的实例
错误 2
连接必须有效且打开。