c#中的Mysql查询

本文关键字:查询 Mysql 中的 | 更新日期: 2023-09-27 18:05:04

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

    Word                       Pos_Score            
    intelligent               .987              
    allows                    .378               
    agree                     .546              
    industries                .289               
    guests                    .874        

使用SELECT查询和WHERE类i比较段落词与数据库文件
段落:

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


c#中的Mysql查询

我想这一定是第一个问题…

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();
    }    
command.CommandText = "SELECT Pos_Score FROM score WHERE Word = " + part;