1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 using System.Data.SqlClient; 11 12 namespace DeleteRecord_10_15 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 public static string constr = "Data Source=WIN-HTT3ODT5GFA;Initial Catalog=language;Intergrated Security=true"; 22 public static SqlConnection con = new SqlConnection(constr); 23 24 private void button1_Click(object sender, EventArgs e) 25 { 26 int wordsID = 0; 27 int pharseID = 0; 28 OpenFileDialog ofd = new OpenFileDialog(); 29 if (ofd.ShowDialog() == DialogResult.OK) 30 { 31 FileStream myfile = new FileStream(ofd.FileName,FileMode.Open); 32 StreamReader myReader = new StreamReader(myfile); 33 string line = myReader.ReadLine(); 34 if ( line.Trim ()!="" &&line.Trim().Contains(" "))//line不为空 qie 为词组 35 { 36 pharseID = GetPharseID(line); 37 if (deleteFromPharseClassification(pharseID)) 38 { 39 if (deleteFromPharse(pharseID)) 40 { 41 MessageBox.Show("删除成功!"); 42 } 43 } 44 } 45 else if (line.Trim() != "" && !line.Trim().Contains(" "))//line不为空 qie 为单词 46 { 47 wordsID = this.GetWordsID(line.Trim()); 48 if (deleteFromWordsclassification(wordsID)) 49 { 50 if (deleteFromWords(wordsID)) 51 { 52 MessageBox.Show("删除成功"); 53 } 54 } 55 } 56 } 57 } 58 59 public int GetWordsID(string strWord) 60 { 61 string sqlcmd2 = "select wordsID from words where word='" + strWord + "'"; 62 SqlCommand cmd2 = new SqlCommand(sqlcmd2, con); 63 con.Open(); 64 SqlDataReader reader = cmd2.ExecuteReader(); 65 int ID = Convert.ToInt32(reader[0].ToString()); 66 reader.Close(); 67 con.Close(); 68 return ID; 69 } 70 71 public int GetPharseID(string strPharse) 72 { 73 string sqlcmd2 = "select pharseID from pharse where pharse='" + strPharse + "'"; 74 SqlCommand cmd2 = new SqlCommand(sqlcmd2, con); 75 con.Open(); 76 SqlDataReader reader = cmd2.ExecuteReader(); 77 int ID = Convert.ToInt32(reader[0].ToString()); 78 reader.Close(); 79 con.Close(); 80 return ID; 81 } 82 83 public Boolean deleteFromWordsclassification(int wordsID) 84 { 85 Boolean flag = false; 86 string sqlcmd1 = "delete from wordsclassification where wordsID=" + wordsID; 87 SqlCommand cmd1 = new SqlCommand(sqlcmd1, con); 88 int num = 0; 89 try 90 { 91 con.Open(); 92 num = cmd1.ExecuteNonQuery(); 93 con.Close(); 94 } 95 catch (Exception ex) 96 { 97 MessageBox.Show(ex.ToString()); 98 } 99 finally 100 { 101 if (num == 1) 102 flag = true; 103 else 104 flag = false; 105 } 106 return flag; 107 } 108 109 public Boolean deleteFromWords(int wordsID) 110 { 111 Boolean flag = false; 112 string sqlcmd1 = "delete from words where wordsID =" + wordsID; 113 SqlCommand cmd1 = new SqlCommand(sqlcmd1, con); 114 int num = 0; 115 try 116 { 117 con.Open(); 118 num = cmd1.ExecuteNonQuery(); 119 con.Close(); 120 } 121 catch (Exception ex) 122 { 123 MessageBox.Show(ex.ToString()); 124 } 125 finally 126 { 127 if (num == 1) 128 flag = true; 129 else 130 flag = false; 131 } 132 return flag; 133 } 134 135 public Boolean deleteFromPharseClassification(int pharseID) 136 { 137 Boolean flag = false; 138 string sqlcmd1 = "delete from pharseClassification where pharseID =" + pharseID; 139 SqlCommand cmd1 = new SqlCommand(sqlcmd1, con); 140 int num = 0; 141 try 142 { 143 con.Open(); 144 num = cmd1.ExecuteNonQuery(); 145 con.Close(); 146 } 147 catch (Exception ex) 148 { 149 MessageBox.Show(ex.ToString()); 150 } 151 finally 152 { 153 if (num == 1) 154 flag = true; 155 else 156 flag = false; 157 } 158 return flag; 159 } 160 161 public Boolean deleteFromPharse(int pharseID) 162 { 163 Boolean flag = false; 164 string sqlcmd1 = "delete from pharse where pharseID =" + pharseID; 165 SqlCommand cmd1 = new SqlCommand(sqlcmd1, con); 166 int num = 0; 167 try 168 { 169 con.Open(); 170 num = cmd1.ExecuteNonQuery(); 171 con.Close(); 172 } 173 catch (Exception ex) 174 { 175 MessageBox.Show(ex.ToString()); 176 } 177 finally 178 { 179 if (num == 1) 180 flag = true; 181 else 182 flag = false; 183 } 184 return flag; 185 } 186 } 187 }
为什么不能运行?为什么?为什么?什么?么?
运行不了?是界面都显示不了?那只有一个原因,数据库连不上报异常了。
但是我设了一个断点,它连form的构造函数都没执行到,我好纠结。。。
@Alloy:
那就是new SqlConnection(constr)这的问题,数据库连不上
@向往-SONG: 哇咔咔,果真是这里出的问题,您真是我的救世主,大神,你收我为徒吧!!
懒得看
你妹
@Alloy: kao 你直接贴出异常代码 不行啊
@随静而动: 要有异常代码,我还问啊,我不会自己解决啊;关键是他不报错,就像是 控制台应用程序最后没写Console.Read()一样,直接一闪而过了。
你新建一个项目,上面安一个Button,然后在后台把代码贴上去,直接运行,看看效果就知道了
@随静而动: 问题解决了,不管怎么样,还是要谢谢你,以后我不会直接贴代码了,希望我遇到问题时,你还能有闲情过来看一下。(*^__^*)
@Alloy: 我要拜你为师