title: Reading Text files and inserting the words present in those files into a table in sql server
body: <p>I have a folder that  contains 25000 text files and i would like to read these files and place the words into table.My text files are named in the following format 1.txt,2.txt,........and so on to 25000.txt. Each text file contains words in the following form.</p>
  
  <pre><code>sample contents of my file
  apple
  cat
  rat
  shoe
  </code></pre>
  
  <p>The words may be repeated in other textfiles too, i want a c# code that could read  the text files identify the words that are repeated and also those that are not repeated and then insert them into database in Sqlserver in the following form.</p>
  
  <pre><code>keyword    document name
  cat        1.txt,2.txt,3.txt
  rat        4.txt,1.txt
  fish       5.txt
  </code></pre>
  
  <p>`    </p>
  
  <pre><code>using System;
  
  using System.Collections.Generic;
  
  using System.ComponentModel;
  
  using System.Data;
  
  using System.Drawing;
  
  using System.Linq;
  
  using System.Text;
  
  
  using System.Windows.Forms;
  
  using System.IO;
  
  using System.Data.SqlClient;
  
  
  
  namespace RAMESH
   {
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void textBox1_TextChanged(object sender, EventArgs e)
      {
  
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
  
          string[] files = Directory.GetFiles(textBox1.Text, "*.txt");
          int i;
          string sqlstmt,str;
          SqlConnection con = new SqlConnection("data source=dell-pc\\sql1; initial         catalog=db; user id=sa; password=a;");
          SqlCommand cmd;
          sqlstmt = "delete from Items";
          cmd = new SqlCommand(sqlstmt, con);
          con.Open();
          cmd.ExecuteNonQuery();
          for (i = 0; i &lt; files.Length; i++)
          {
              StreamReader sr = new StreamReader(files[i]);
              FileInfo f = new FileInfo(files[i]);
              string fname;
              fname = f.Name;
              fname = fname.Substring(0, fname.LastIndexOf('.'));
              //MessageBox.Show(fname);
              while ((str = sr.ReadLine()) != null)
              {
                  int nstr=1;
                  //int x,y;
                  //for (x = 0; x &lt; str.Length; x++)
                  //{ 
                  //    y = Convert.ToInt32(str.Substring(x,1));
                  //    if ((y &lt; 48 &amp;&amp; y &gt; 75) || (y &lt; 65 &amp;&amp; y &gt; 97) || (y &lt; 97 &amp;&amp; y &gt; 122)) ;
                  //}
                  sqlstmt = "insert into Items values('" + str + "','" + fname + "')";
                  cmd = new SqlCommand(sqlstmt, con);                    
                  try
                  {
                      cmd.ExecuteNonQuery();
                  }
                  catch (Exception ex)
                  {
                      sqlstmt = "update Items set docname=docname + '," + fname + "'   where itemname='" + str + "'";
                      cmd = new SqlCommand(sqlstmt, con);
                      cmd.ExecuteNonQuery();
                  }
              }
              sr.Close();
          }
          MessageBox.Show("keywords added successfully");
          con.Close();
      }
  }
  </code></pre>
  
  <p>}
  `</p>