这是我的代码...它给出了一个带有文档的不同查询的余弦相似度值的排序(降序)列表。
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);
}
这是数据插入方法......
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.");
}
}
我想将数据库中top(最高余弦相似度值)查询的值与string document
存储在同一个表中...
表结构如下......
--------------------------------------------------------------------------------
id | pid | p_title | p_author | p_year | p_venue | p_abstract | research_area
--------------------------------------------------------------------------------
字符串文档为p_abstract
,查询将保存在research_area
列
任何建议都将受到高度赞赏。
答案 0 :(得分:0)
首先进行一些代码审查:
write_To_Database
,r_field
,research_field
,top_value
等。r_field
,abstrct
。SqlConnection
,SqlCommand
等应始终为encapsulated in a using,以便妥善处理。p_abstract = " + document;
,始终使用参数。否则,您可能会冒SQL injection。SELECT
,然后做INSERT
?为什么不简单地将WHERE
添加到INSERT
?research_area
vs research_field
,document
vs abstrct
vs p_abstract
?现在,这是您的主要问题:您执行query.Parameters.Add
但是无处添加值。它很简单:cmd.Parameters.Add("@research_field", SqlDbType.Text).Value = research_field;
。
但是,我对您的代码逻辑感到有些困惑:首先,您要查找记录的ID,然后插入带有该ID的记录?我发现这是非常可疑的,因为我认为ID必须是唯一的。