如何将单列写入数据库 sql 服务器

How to write a single column into database sql server

我有执行以下步骤的代码: 1. Selects texts from SQL database table (column name i.e. p_abstract) 2. Stores them in a List<string> 3. Removes stopWords from these texts

这是代码

try
{
    string[] stopWords = File.ReadAllLines(@"C:\Users\~\Desktop\stopWords_E_Unique_Spaces.txt");

    SqlConnection con = new SqlConnection("Data Source=Source_Name;Initial Catalog=subset_aminer;Integrated Security=True");

    con.Open();
    SqlCommand query = con.CreateCommand();
    query.CommandText = "select p_abstract from sub_aminer_paper where aid = 52883";

    SqlDataReader reader = query.ExecuteReader();

    var summary = new List<string>();
     while(reader.Read())
         summary.Add(reader["p_abstract"].ToString());

    reader.Close();
    string[] input_Text = summary.ToArray();

    List<string> result = new List<string>();
    for (int i = 0; i < input_Text.Length; i++)
    {
          foreach (string word in stopWords)
          {
               input_Text[i] = input_Text[i].ToLower().Replace(word, " ").Trim();
               input_Text[i] = Regex.Replace(input_Text[i], @"\s+", " ");
          }

          result.Add(input_Text[i]);
    }

    /*foreach (string l in result)
          Console.WriteLine(l);*/

}

现在我想将这些已删除的停用词写回相同的 table 新创建的空列中,即 p_abstract_SWR 针对相同的文本。

语法可能有点偏差,但不多,

    List<string[]> words1 = new List<string[]>();
    string[] stopWords = File.ReadAllLines(@"C:\Users\~\Desktop\stopWords_E_Unique_Spaces.txt");
    Using(SqlConnection con = new SqlConnection("Data Source=Source_Name;Initial Catalog=subset_aminer;Integrated Security=True"))
    {
        con.Open();
        using (SqlCommand query = con.CreateCommand())
        {
         query.CommandText = "select p_abstract from sub_aminer_paper where aid = 52883";
         SqlDataReader reader = query.ExecuteReader();          
         while (reader.Read())
         {
             string[] ListElements = new string[2];
             ListElements[0]=reader["p_abstract"].ToString();
             ListElements[1]=reader["p_abstract"].ToString() ;
             words1.Add(ListElements);              
         }
        }
    }

    reader.Close();
} 
for  (int index = 0;    index<words1.Count ; index ++ ) 
{ 
    foreach  (string word in  stopWords ) 
    {
         var item = words1.ElementAt(index);
         item[1] =  item[1].ToString ( ).ToLower().Replace(word,  " " ).Trim();
         item[1] =  Regex.Replace (item[1].ToString ( ) ,  @"\s+" ,  " " ) ; 
    } 
} 
using(SQLConnection con = new SQLConnection("Data Source=Source_Name;Initial Catalog=subset_aminer;Integrated Security=True")) 
{
  con.Open( ) ; 
  using(SQLCommand command = new SQLCommand(con) )
  { 
    for (int i = 0;i<words1.Count ; i++ )
    {
    var item = words1.ElementAt(i);
    var itemKey = item[0].ToString();
    var itemValue = item[1].ToString();
    command.CommandText =  string.Format( "UPDATE table1 SET p_abstract_SWR=@newvalue where p_abstract=@oldvalue");
    command.Parameters.AddWithValue( "@newvalue" , itemValue ) ; 
    command.Parameters.AddWithValue( "@oldvalue" , itemKey ); 
    command.ExecuteNonQuery ( ) ;
    } 
  }
 con.Close ( ) ;
} 

在列表中将数据库中的值存储在数组的 0 和 1 位置,然后在 1 上执行替换,这将作为一对旧值和新值,可用于根据旧值更新新值值。

改进 learningNew 的答案后,这里是功能齐全的答案,代码正确,我测试过它工作正常。

 try {
                List<string[]> words1 = new List<string[]>();
                string[] stopWords = File.ReadAllLines(@"C:\Users\~\Desktop\stopWords_E_Unique_Spaces.txt");

                using (SqlConnection con = new SqlConnection("Data Source=Source_Name;Initial Catalog=subset_aminer;Integrated Security=True"))
                {
                    con.Open();
                    using (SqlCommand query = con.CreateCommand())
                    {
                        query.CommandText = "select p_abstract from sub_aminer_paper where aid = 52883";
                        SqlDataReader reader = query.ExecuteReader();
                        while (reader.Read())
                        {
                            string[] ListElements = new string[2];
                            ListElements[0] = reader["p_abstract"].ToString();
                            ListElements[1] = reader["p_abstract"].ToString();
                            words1.Add(ListElements);
                        }
                        reader.Close();
                    }
                }
                for (int index = 0; index < words1.Count; index++)
                {
                    foreach (string word in stopWords)
                    {
                        var item = words1.ElementAt(index);
                        item[1] = item[1].ToString().ToLower().Replace(word, " ").Trim();
                        item[1] = Regex.Replace(item[1].ToString(), @"\s+", " ");
                    }
                }
                using (SqlConnection con = new SqlConnection("Data Source=Source_Name;Initial Catalog=subset_aminer;Integrated Security=True"))
                {
                    con.Open();

                        for (int i = 0; i < words1.Count; i++)
                        {
                            var item      = words1.ElementAt(i);
                            var itemKey   = item[0].ToString();
                            var itemValue = item[1].ToString();

                            string query = "select id from sub_aminer_paper where p_abstract LIKE @p_abstract and aid = 52883";

                            using (SqlCommand cmd = new SqlCommand(query, con))
                            {
                                //string inp = input;
                                cmd.Parameters.AddWithValue("@p_abstract", itemKey);

                                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 p_abstract_SWR = @p_abstract_SWR where id = @id";
                            int res = 0;
                            using (SqlCommand cmd_update = new SqlCommand(update_query, con))
                            {
                                int identity = id;
                                string p_abstractSWR = itemValue;

                                cmd_update.Parameters.AddWithValue("@id", identity);
                                cmd_update.Parameters.AddWithValue("@p_abstract_SWR", p_abstractSWR);

                                res = cmd_update.ExecuteNonQuery();
                            } 
                        }
                    }
                    con.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }