while循环不适用于tupled list c#db data update

时间:2015-06-26 11:50:22

标签: c# string list sql-update tuples

这是我在迭代while循环时调用插入方法的代码

Program p = new Program();
            var lines = System.IO.File.ReadLines(@"C:\Users\Malik\Desktop\research_fields.txt");
            var dd = new List<Tuple<string, double, string>>();
try
            {
                SqlConnection con = new SqlConnection("Data Source=KHIZER;Initial Catalog=subset_aminer;Integrated Security=True");

                con.Open();
SqlCommand query = con.CreateCommand();
                query.CommandText = "select p_abstract from sub_aminer_paper where id between 1 and 500 and DATALENGTH(p_abstract) != 0";

                SqlDataReader reader = query.ExecuteReader();

                string summary = null;

                while (reader.Read())
                {
                    summary = reader["p_abstract"].ToString();
                    dd.AddRange(lines.Select(line => Tuple.Create(line, p.calculate_CS(line, summary), summary)));

                    var top_value = dd.OrderByDescending(x => x.Item2).FirstOrDefault();

                    if (top_value != null && top_value.Item2 > 0)
                    {
                        // look up record using top_value.Item3, and then store top_value.Item1
                        var abstrct = top_value.Item3.ToString();
                        var r_field = top_value.Item1.ToString();



                       write_To_Database(abstrct, r_field);
                        }

                    }
                    reader.Close();
public static void write_To_Database(string document, string research_field)
        {
            int result = 0; 
            try
             {
                 string connection = "Data Source=KHIZER;Initial Catalog=subset_aminer;Integrated Security=True;MultipleActiveResultSets=True;";

                 using (SqlConnection con = new SqlConnection(connection))
                 {
                     con.Open();

                     string query = "select id from sub_aminer_paper where pid between 1 and 500 and DATALENGTH(p_abstract) != 0 and p_abstract LIKE @p_abstract";
                     using (SqlCommand cmd = new SqlCommand(query, con))
                     {
                         string st = document;
                         cmd.Parameters.AddWithValue("@p_abstract", st);

                         int id = 0;
                         using (SqlDataReader reader = cmd.ExecuteReader())
                         {
                             while (reader.Read())
                             {
                                 id = reader.GetInt32(0);
                             }

                             reader.Close();

                             string update_query = "update sub_aminer_paper set research_area = @research_area where id = @id";
                             using (SqlCommand cmd_update = new SqlCommand(update_query, con))
                             {
                                 int identity = id;
                                 string r_field = research_field;

                                 cmd_update.Parameters.AddWithValue("@id", identity);
                                 cmd_update.Parameters.AddWithValue("@research_area", r_field);
                                 //cmd_update.CommandTimeout = 20;
                                 result = cmd_update.ExecuteNonQuery();
                             }
                         }
                     }
                     con.Close();
                 }  
             }
             catch (Exception e)
             {
               Console.WriteLine("Exception: " + e.Message);
             }
             finally
             {
               Console.WriteLine("Executing finally block.");
             }
        }
public double calculate_CS(string doc, string query)
        {
            var result = getContent(doc, query);


            var length = result.GetLength(0);


            double[] doc1Array = new double[length];
            double[] doc2Array = new double[length];

            //first doc
            for (int i = 0; i < length; i++)
            {
                doc1Array[i] = result[i, 0];
            }

            //second doc
            for (int i = 0; i < length; i++)
            {
                doc2Array[i] = result[i, 1];
            }

            var cosSimilarity = CalculateCosineSimilarity(doc1Array, doc2Array);


            return cosSimilarity;
        }

这看起来很好,但问题是它会更新变量字符串summary的每次值,并且它还会在summary此语句中使用dd.AddRange(lines.Select(line => Tuple.Create(line, p.calculate_CS(line, summary), summary)));的更新值(我在调试时已经检查过)但它在语句summary中经过3次或4次迭代后使用相同的var top_value = dd.OrderByDescending(x => x.Item2).FirstOrDefault();值,因此在数据库中不会导致数据更新,因为再次使用summary的相同值。 此外,它在第7次迭代中需要summary的另一个更新值并正确插入数据。但在此之后var top_value = dd.OrderByDescending(x => x.Item2).FirstOrDefault();这个陈述有同样的价值。

数据库中的结果更新如下。很明显,前四次迭代工作,然后迭代次数7工作,然后根本没有工作或更新,等等。

id research_area
-------------------------
067入侵检测系统
078数据流管理
092数据流管理
093查询处理
102 NULL
169 NULL
170入侵检测系统
171 NULL
172 NULL
173 NULL
174 NULL
175 NULL
176 NULL

任何帮助和建议都将受到高度赞赏。 感谢

0 个答案:

没有答案