1

我要编写一个程序来查找段落的分数。数据库文件有一个单词分数。
数据库文件

    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
连接必须有效且打开。

4

2 回答 2

1

我认为这一定是第一个问题...

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;
    MySqlConnection connection = new MySqlConnection(MyConString);
    connection.Open();
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        float pos1 = 0;
        while ((line = reader.ReadLine()) != null)
        {
            string[] parts = line.Split(' ');


            foreach (string part in parts)
            {

                command.CommandText = "SELECT Pos_Score FROM score WHERE Word = @part";
                command.Parameters.Add("@part",MySqlDBtype.Varchar).Value = part;
                try
                {
                    amd = (float)command.ExecuteScalar();
                    pos1 = amd + pos1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                Reader = command.ExecuteReader();

            }
        }
        MessageBox.Show("The Positive score of Sentence is="+pos1.ToString());
        reader.Close();
        connection.Close();
    }    
于 2013-03-20T08:42:55.660 回答
0
command.CommandText = "SELECT Pos_Score FROM score WHERE Word = " + part;
于 2013-03-21T06:51:54.993 回答