[英]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: 首先回顾一下代码:
write_To_Database
, r_field
, research_field
, top_value
, etc. 不要在名称中使用下划线: write_To_Database
, r_field
, research_field
, top_value
等。 r_field
, abstrct
. 不要缩写: r_field
, abstrct
。 SqlConnection
, SqlCommand
, etc. should always be encapsulated in a using so they get properly disposed of. SqlConnection
, SqlCommand
等应始终封装在using中,以便正确处理它们。 p_abstract = " + document;
, always work with parameters. Otherwise you risk SQL injection . 不要这样做: p_abstract = " + document;
始终使用参数。否则会冒着SQL注入的风险。 SELECT
first and an INSERT
after? 为什么要先执行SELECT
然后再INSERT
? Why not simply add that WHERE
to your INSERT
? 为什么不简单地将WHERE
添加到您的INSERT
呢? research_area
vs research_field
, document
vs abstrct
vs p_abstract
? 为什么您的名字不一致: research_area
vs research_field
, document
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.