日期:2014-05-17 浏览次数:20947 次
namespace DeleteRecord_10_15 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static string constr = "Data Source=*******;Initial Catalog=language;Intergrated Security=true"; public static SqlConnection con = new SqlConnection(constr); private void button1_Click(object sender, EventArgs e) { int wordsID = 0; int pharseID = 0; OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { FileStream myfile = new FileStream(ofd.FileName,FileMode.Open); StreamReader myReader = new StreamReader(myfile); string line = myReader.ReadLine(); if ( line.Trim ()!="" &&line.Trim().Contains(" "))//line不为空 qie 为词组 { pharseID = GetPharseID(line); if (deleteFromPharseClassification(pharseID)) { if (deleteFromPharse(pharseID)) { MessageBox.Show("删除成功!"); } } } else if (line.Trim() != "" && !line.Trim().Contains(" "))//line不为空 qie 为单词 { wordsID = this.GetWordsID(line.Trim()); if (deleteFromWordsclassification(wordsID)) { if (deleteFromWords(wordsID)) { MessageBox.Show("删除成功"); } } } } } public int GetWordsID(string strWord) { string sqlcmd2 = "select wordsID from words where word='" + strWord + "'"; SqlCommand cmd2 = new SqlCommand(sqlcmd2, con); con.Open(); SqlDataReader reader = cmd2.ExecuteReader(); int ID = Convert.ToInt32(reader[0].ToString()); reader.Close(); con.Close(); return ID; } public int GetPharseID(string strPharse) { string sqlcmd2 = "select pharseID from pharse where pharse='" + strPharse + "'"; SqlCommand cmd2 = new SqlCommand(sqlcmd2, con); con.Open(); SqlDataReader reader = cmd2.ExecuteReader(); int ID = Convert.ToInt32(reader[0].ToString()); reader.Close(); con.Close(); return ID; } public Boolean deleteFromWordsclassification(int wordsID) { Boolean flag = false; string sqlcmd1 = "delete from wordsclassification where wordsID=" + wordsID; SqlCommand cmd1 = new SqlCommand(sqlcmd1, con); int num = 0; try { con.Open(); num = cmd1.ExecuteNonQuery(); con.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (num == 1) flag = true; else flag = false; } return flag; } public Boolean deleteFromWords(int wordsID) { Boolean flag = false; string sqlcmd1 = "delete from words where wordsID =" + wordsID; SqlCommand cmd1 = new SqlCommand(sqlcmd1, con); int num = 0; try { con.Open(); num = cmd1.ExecuteNonQuery();