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

私はこれが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 に答える