简体   繁体   English

与本地字符串值c#比较后如何在sql server表中存储值

[英]how to store value in sql server table after comparing with local string value c#

Here is my code...which gives a sorted (descending) list of cosine similarity values of different queries with a document. 这是我的代码...给出了文档中不同查询的余弦相似度值的排序列表(降序)。

   var dd = new List<Tuple<string, double, string>>();

            while ((line = file.ReadLine()) != null)
            {
                dd.Add(Tuple.Create(line, p.calculate_CS(line, document), document));
            }

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

            if (top_value != null)
            { 
                // 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);
            }

This is the data insertion method...... 这是数据插入方法……

static void write_To_Database(string document, string research_field)
        {
            try
            {
                SqlConnection con = new SqlConnection("Data Source=KHIZER;Initial Catalog=subset_aminer;Integrated Security=True;");

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

                SqlDataReader reader = query.ExecuteReader();


                reader.Read();
                int id = reader.GetInt32(0);
                //reader.Close();

                query.Parameters.Add("@research_field", SqlDbType.Text);
                query.Parameters.Add("@id", SqlDbType.Int);

                query.CommandText = "insert into sub_aminer_paper (research_area) values(@research_field) where id = @id";
                query.ExecuteNonQuery();


                con.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");

            }
        }

I want to store the value of top (highest cosine similarity value) query in database against the string document which is taken from the same table... Table structure is as follows... 我想将取自同一张表的string document的top(最高余弦相似度值)查询的值存储在数据库中。表结构如下...

--------------------------------------------------------------------------------
id | pid | p_title | p_author | p_year | p_venue | p_abstract | research_area
--------------------------------------------------------------------------------

string document is p_abstract and query is to be saved in the column research_area 字符串文档为p_abstract ,查询将保存在research_area列中

Any suggestions will be highly appreciated. 任何建议将不胜感激。

First a bit of code review: 首先回顾一下代码:

  • Don't use underscores in names: write_To_Database , r_field , research_field , top_value , etc. 不要在名称中使用下划线: write_To_Databaser_fieldresearch_fieldtop_value等。
  • Don't abbreviate: r_field , abstrct . 不要缩写: r_fieldabstrct
  • SqlConnection , SqlCommand , etc. should always be encapsulated in a using so they get properly disposed of. SqlConnectionSqlCommand等应始终封装在using中,以便正确处理它们。
  • Don't do this: p_abstract = " + document; , always work with parameters. Otherwise you risk SQL injection . 不要这样做: p_abstract = " + document; 始终使用参数。否则会冒着SQL注入的风险。
  • Why do you do a SELECT first and an INSERT after? 为什么要先执行SELECT然后再INSERT Why not simply add that WHERE to your INSERT ? 为什么不简单地将WHERE添加到您的INSERT呢?
  • Why are your names not consistent: research_area vs research_field , document vs abstrct vs p_abstract ? 为什么您的名字不一致: research_area vs research_fielddocument vs abstrct vs p_abstract

Now here's your main problem: you do query.Parameters.Add yet nowhere do you add the value. 现在,这是您的主要问题:您执行query.Parameters.Add但没有在任何地方添加值。 It's as simple as this: cmd.Parameters.Add("@research_field", SqlDbType.Text).Value = research_field; 就是这样简单: cmd.Parameters.Add("@research_field", SqlDbType.Text).Value = research_field; .

However, I'm a bit baffled by the logic of your code: first you go looking for an ID of a record, and then you insert a record with that ID? 但是,我对代码的逻辑有些困惑:首先要查找记录的ID,然后再插入具有该ID的记录? I find this highly suspect, since I would assume that the ID needs to be unique. 我认为这很可疑,因为我认为ID需要唯一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检索SQL Server表的列值并将其存储在label.C#ASP.Net中的文本 - How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net 如何使用C#将复选框值存储到SQL Server数据库中 - How to store a checkbox value into sql server database using c# 将C#datetime值与SQL Server Compact 4 datetime进行比较 - Comparing C# datetime value to SQL Server Compact 4 datetime 将值插入 C# 中的 SQL 服务器表后,如何将值加 1? - How do I add 1 to a value after it's inserted into SQL Server table in C#? 如何将行值从sql server转换为c#中的字符串 - how to convert row value from sql server to string in c# 如何检索SQL Server 2005表的列值并将其存储在label.C#ASP.Net中的文本 - How to retrieve column value of sql server 2005 table and store it in label.Text of c# ASP.Net 将C#中json上变量的值与字符串进行比较 - Comparing the value of a variable on json in C# to a string 如何使用 sql 查询在 SQL Server 中存储来自 C# 的哈希值 - How to store hash value from C# in SQL Server using sql query 如何在C#中将MySqlCommand的值存储到本地变量? - How can I store the value of a MySqlCommand in C# to a local variable? C# 和 SQL Server:如果字符串值为空,如何在命令参数中插入 DBNull.Value? - C# & SQL Server : how to Insert DBNull.Value in command parameter if string value is empty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM