Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 23k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.OleDb;
  8. namespace ch6_3
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Data.OleDb.OleDbConnection oleDbConnection1;
  16. private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
  17. private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
  18. private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
  19. private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
  20. private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
  21. private System.Windows.Forms.TextBox textBox1;
  22. private System.Windows.Forms.TextBox textBox2;
  23. private System.Windows.Forms.TextBox textBox3;
  24. private System.Windows.Forms.TextBox textBox4;
  25. private System.Windows.Forms.Label label1;
  26. private System.Windows.Forms.Label label2;
  27. private System.Windows.Forms.Label label3;
  28. private System.Windows.Forms.Label label4;
  29. private System.Windows.Forms.GroupBox groupBox1;
  30. private System.Windows.Forms.Button button1;
  31. private System.Windows.Forms.Button button2;
  32. private System.Windows.Forms.Button button3;
  33. private System.Windows.Forms.Button button4;
  34. private System.Windows.Forms.Button button5;
  35. private System.Windows.Forms.Button button6;
  36. private BindingManagerBase myBind;
  37. private System.Data.DataSet dataSet1;
  38. private System.Windows.Forms.Button button7;
  39. private System.Windows.Forms.Button button8;
  40. /// <summary>
  41. /// 必需的设计器变量。
  42. /// </summary>
  43. private System.ComponentModel.Container components = null;
  44. public Form1()
  45. {
  46. //
  47. // Windows 窗体设计器支持所必需的
  48. //
  49. InitializeComponent();
  50. DataBinding();
  51. //
  52. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  53. //
  54. }
  55. private void DataBinding()
  56. {
  57. //刷新数据集
  58. this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
  59. //数据绑定
  60. this.textBox1.DataBindings.Add("Text",this.dataSet1,"information.姓名");
  61. this.textBox2.DataBindings.Add("Text",this.dataSet1,"information.年龄");
  62. this.textBox3.DataBindings.Add("Text",this.dataSet1,"information.电话");
  63. this.textBox4.DataBindings.Add("Text",this.dataSet1,"information.电子邮件");
  64. //构造
  65. myBind=this.BindingContext[this.dataSet1,"information"];
  66.    
  67. }
  68. /// <summary>
  69. /// 清理所有正在使用的资源。
  70. /// </summary>
  71. protected override void Dispose( bool disposing )
  72. {
  73. if( disposing )
  74. {
  75. if (components != null) 
  76. {
  77. components.Dispose();
  78. }
  79. }
  80. base.Dispose( disposing );
  81. }
  82. #region Windows Form Designer generated code
  83. /// <summary>
  84. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  85. /// 此方法的内容。
  86. /// </summary>
  87. private void InitializeComponent()
  88. {
  89. this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
  90. this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
  91. this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
  92. this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
  93. this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
  94. this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
  95. this.textBox1 = new System.Windows.Forms.TextBox();
  96. this.textBox2 = new System.Windows.Forms.TextBox();
  97. this.textBox3 = new System.Windows.Forms.TextBox();
  98. this.textBox4 = new System.Windows.Forms.TextBox();
  99. this.label1 = new System.Windows.Forms.Label();
  100. this.label2 = new System.Windows.Forms.Label();
  101. this.label3 = new System.Windows.Forms.Label();
  102. this.label4 = new System.Windows.Forms.Label();
  103. this.groupBox1 = new System.Windows.Forms.GroupBox();
  104. this.button1 = new System.Windows.Forms.Button();
  105. this.button2 = new System.Windows.Forms.Button();
  106. this.button3 = new System.Windows.Forms.Button();
  107. this.button4 = new System.Windows.Forms.Button();
  108. this.button5 = new System.Windows.Forms.Button();
  109. this.button6 = new System.Windows.Forms.Button();
  110. this.dataSet1 = new System.Data.DataSet();
  111. this.button7 = new System.Windows.Forms.Button();
  112. this.button8 = new System.Windows.Forms.Button();
  113. this.groupBox1.SuspendLayout();
  114. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
  115. this.SuspendLayout();
  116. // 
  117. // oleDbConnection1
  118. // 
  119. this.oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=D:Documents and Settingsadministrator.TRINCE-JULIEMy Documentsperson.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
  120. // 
  121. // oleDbDataAdapter1
  122. // 
  123. this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
  124. this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
  125. this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
  126. this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
  127. new System.Data.Common.DataTableMapping("Table", "information", new System.Data.Common.DataColumnMapping[] {
  128.    new System.Data.Common.DataColumnMapping("姓名", "姓名"),
  129.    new System.Data.Common.DataColumnMapping("年龄", "年龄"),
  130.    new System.Data.Common.DataColumnMapping("电话", "电话"),
  131.    new System.Data.Common.DataColumnMapping("电子邮件", "电子邮件")})});
  132. this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
  133. // 
  134. // oleDbDeleteCommand1
  135. // 
  136. this.oleDbDeleteCommand1.CommandText = "DELETE FROM information WHERE (姓名 = ?) AND (电子邮件 = ? OR ? IS NULL AND 电子邮件 IS NUL" +
  137. "L) AND (电话 = ? OR ? IS NULL AND 电话 IS NULL)";
  138. this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
  139. this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_姓名", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "姓名", System.Data.DataRowVersion.Original, null));
  140. this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电子邮件", System.Data.DataRowVersion.Original, null));
  141. this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电子邮件1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电子邮件", System.Data.DataRowVersion.Original, null));
  142. this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电话", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电话", System.Data.DataRowVersion.Original, null));
  143. this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电话1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电话", System.Data.DataRowVersion.Original, null));
  144. // 
  145. // oleDbInsertCommand1
  146. // 
  147. this.oleDbInsertCommand1.CommandText = "INSERT INTO information(电话, 电子邮件, 年龄, 姓名) VALUES (?, ?, ?, ?)";
  148. this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
  149. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 50, "电话"));
  150. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, "电子邮件"));
  151. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("年龄", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "年龄", System.Data.DataRowVersion.Current, null));
  152. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("姓名", System.Data.OleDb.OleDbType.VarWChar, 50, "姓名"));
  153. // 
  154. // oleDbSelectCommand1
  155. // 
  156. this.oleDbSelectCommand1.CommandText = "SELECT 电话, 电子邮件, 年龄, 姓名 FROM information";
  157. this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
  158. // 
  159. // oleDbUpdateCommand1
  160. // 
  161. this.oleDbUpdateCommand1.CommandText = "UPDATE information SET 电话 = ?, 电子邮件 = ?, 年龄 = ?, 姓名 = ? WHERE (姓名 = ?) AND (电子邮件 " +
  162. "= ? OR ? IS NULL AND 电子邮件 IS NULL) AND (电话 = ? OR ? IS NULL AND 电话 IS NULL)";
  163. this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
  164. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 50, "电话"));
  165. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, "电子邮件"));
  166. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("年龄", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "年龄", System.Data.DataRowVersion.Current, null));
  167. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("姓名", System.Data.OleDb.OleDbType.VarWChar, 50, "姓名"));
  168. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_姓名", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "姓名", System.Data.DataRowVersion.Original, null));
  169. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电子邮件", System.Data.DataRowVersion.Original, null));
  170. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电子邮件1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电子邮件", System.Data.DataRowVersion.Original, null));
  171. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电话", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电话", System.Data.DataRowVersion.Original, null));
  172. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_电话1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "电话", System.Data.DataRowVersion.Original, null));
  173. // 
  174. // textBox1
  175. // 
  176. this.textBox1.Location = new System.Drawing.Point(104, 32);
  177. this.textBox1.Name = "textBox1";
  178. this.textBox1.Size = new System.Drawing.Size(160, 21);
  179. this.textBox1.TabIndex = 0;
  180. this.textBox1.Text = "";
  181. // 
  182. // textBox2
  183. // 
  184. this.textBox2.Location = new System.Drawing.Point(104, 64);
  185. this.textBox2.Name = "textBox2";
  186. this.textBox2.Size = new System.Drawing.Size(160, 21);
  187. this.textBox2.TabIndex = 1;
  188. this.textBox2.Text = "";
  189. // 
  190. // textBox3
  191. // 
  192. this.textBox3.Location = new System.Drawing.Point(104, 96);
  193. this.textBox3.Name = "textBox3";
  194. this.textBox3.Size = new System.Drawing.Size(160, 21);
  195. this.textBox3.TabIndex = 2;
  196. this.textBox3.Text = "";
  197. // 
  198. // textBox4
  199. // 
  200. this.textBox4.Location = new System.Drawing.Point(104, 128);
  201. this.textBox4.Name = "textBox4";
  202. this.textBox4.Size = new System.Drawing.Size(160, 21);
  203. this.textBox4.TabIndex = 3;
  204. this.textBox4.Text = "";
  205. // 
  206. // label1
  207. // 
  208. this.label1.Location = new System.Drawing.Point(24, 32);
  209. this.label1.Name = "label1";
  210. this.label1.Size = new System.Drawing.Size(56, 16);
  211. this.label1.TabIndex = 4;
  212. this.label1.Text = "姓名";
  213. // 
  214. // label2
  215. // 
  216. this.label2.Location = new System.Drawing.Point(24, 64);
  217. this.label2.Name = "label2";
  218. this.label2.Size = new System.Drawing.Size(72, 16);
  219. this.label2.TabIndex = 5;
  220. this.label2.Text = "年龄";
  221. // 
  222. // label3
  223. // 
  224. this.label3.Location = new System.Drawing.Point(24, 96);
  225. this.label3.Name = "label3";
  226. this.label3.Size = new System.Drawing.Size(72, 16);
  227. this.label3.TabIndex = 6;
  228. this.label3.Text = "电话";
  229. // 
  230. // label4
  231. // 
  232. this.label4.Location = new System.Drawing.Point(24, 128);
  233. this.label4.Name = "label4";
  234. this.label4.Size = new System.Drawing.Size(72, 16);
  235. this.label4.TabIndex = 7;
  236. this.label4.Text = "电子邮件";
  237. // 
  238. // groupBox1
  239. // 
  240. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  241. this.textBox2,
  242. this.textBox1,
  243. this.textBox3,
  244. this.textBox4,
  245. this.label1,
  246. this.label2,
  247. this.label3,
  248. this.label4});
  249. this.groupBox1.Location = new System.Drawing.Point(16, 8);
  250. this.groupBox1.Name = "groupBox1";
  251. this.groupBox1.Size = new System.Drawing.Size(280, 176);
  252. this.groupBox1.TabIndex = 8;
  253. this.groupBox1.TabStop = false;
  254. this.groupBox1.Text = "我的朋友";
  255. // 
  256. // button1
  257. // 
  258. this.button1.Location = new System.Drawing.Point(40, 200);
  259. this.button1.Name = "button1";
  260. this.button1.Size = new System.Drawing.Size(64, 23);
  261. this.button1.TabIndex = 9;
  262. this.button1.Text = "最前";
  263. this.button1.Click += new System.EventHandler(this.button1_Click);
  264. // 
  265. // button2
  266. // 
  267. this.button2.Location = new System.Drawing.Point(104, 200);
  268. this.button2.Name = "button2";
  269. this.button2.Size = new System.Drawing.Size(64, 23);
  270. this.button2.TabIndex = 10;
  271. this.button2.Text = "前一个";
  272. this.button2.Click += new System.EventHandler(this.button2_Click);
  273. // 
  274. // button3
  275. // 
  276. this.button3.Location = new System.Drawing.Point(168, 200);
  277. this.button3.Name = "button3";
  278. this.button3.Size = new System.Drawing.Size(64, 23);
  279. this.button3.TabIndex = 11;
  280. this.button3.Text = "后一个";
  281. this.button3.Click += new System.EventHandler(this.button3_Click);
  282. // 
  283. // button4
  284. // 
  285. this.button4.Location = new System.Drawing.Point(232, 200);
  286. this.button4.Name = "button4";
  287. this.button4.Size = new System.Drawing.Size(64, 23);
  288. this.button4.TabIndex = 12;
  289. this.button4.Text = "最后";
  290. this.button4.Click += new System.EventHandler(this.button4_Click);
  291. // 
  292. // button5
  293. // 
  294. this.button5.Location = new System.Drawing.Point(168, 248);
  295. this.button5.Name = "button5";
  296. this.button5.Size = new System.Drawing.Size(64, 23);
  297. this.button5.TabIndex = 13;
  298. this.button5.Text = "添加记录";
  299. this.button5.Click += new System.EventHandler(this.button5_Click);
  300. // 
  301. // button6
  302. // 
  303. this.button6.Location = new System.Drawing.Point(232, 248);
  304. this.button6.Name = "button6";
  305. this.button6.Size = new System.Drawing.Size(64, 23);
  306. this.button6.TabIndex = 14;
  307. this.button6.Text = "删除记录";
  308. this.button6.Click += new System.EventHandler(this.button6_Click);
  309. // 
  310. // dataSet1
  311. // 
  312. this.dataSet1.DataSetName = "NewDataSet";
  313. this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
  314. // 
  315. // button7
  316. // 
  317. this.button7.Location = new System.Drawing.Point(40, 248);
  318. this.button7.Name = "button7";
  319. this.button7.Size = new System.Drawing.Size(64, 23);
  320. this.button7.TabIndex = 15;
  321. this.button7.Text = "新建";
  322. this.button7.Click += new System.EventHandler(this.button7_Click);
  323. // 
  324. // button8
  325. // 
  326. this.button8.Location = new System.Drawing.Point(104, 248);
  327. this.button8.Name = "button8";
  328. this.button8.Size = new System.Drawing.Size(64, 23);
  329. this.button8.TabIndex = 16;
  330. this.button8.Text = "修改";
  331. this.button8.Click += new System.EventHandler(this.button8_Click);
  332. // 
  333. // Form1
  334. // 
  335. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  336. this.ClientSize = new System.Drawing.Size(312, 301);
  337. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  338.   this.button8,
  339.   this.button7,
  340.   this.button6,
  341.   this.button5,
  342.   this.button4,
  343.   this.button3,
  344.   this.button2,
  345.   this.button1,
  346.   this.groupBox1});
  347. this.Name = "Form1";
  348. this.Text = "我的通讯录";
  349. this.groupBox1.ResumeLayout(false);
  350. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
  351. this.ResumeLayout(false);
  352. }
  353. #endregion
  354. /// <summary>
  355. /// 应用程序的主入口点。
  356. /// </summary>
  357. [STAThread]
  358. static void Main() 
  359. {
  360. Application.Run(new Form1());
  361. }
  362. private void button1_Click(object sender, System.EventArgs e)
  363. {
  364. //指向第一条记录
  365. this.myBind.Position=0;
  366. }
  367. private void button2_Click(object sender, System.EventArgs e)
  368. {
  369. //如果不是最前一条记录,则指针向前移动
  370. if (this.myBind.Position==0)
  371. {
  372. MessageBox.Show("没有前一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information);
  373. }
  374. else
  375. {
  376. this.myBind.Position--;
  377. }
  378. }
  379. private void button3_Click(object sender, System.EventArgs e)
  380. {
  381. //如果不是最后一条记录,则指针向后移动
  382. if (this.myBind.Position==this.myBind.Count-1)
  383. {
  384. MessageBox.Show("没有后一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information);
  385. }
  386. else
  387. {
  388. this.myBind.Position++;;
  389. }
  390. }
  391. private void button4_Click(object sender, System.EventArgs e)
  392. {
  393. //指向最后一条记录
  394. this.myBind.Position=this.myBind.Count-1;
  395. }
  396. private void button5_Click(object sender, System.EventArgs e)
  397. {
  398. try
  399. {
  400. //首先判断添加的信息是否完整
  401. if (this.textBox1.Text!="" & this.textBox2.Text!="" & this.textBox3.Text!="" & this.textBox4.Text!="")
  402. {
  403. //添加记录时的SQL命令
  404. string insert= " INSERT INTO information( 姓名,年龄 , 电话 ,电子邮件) VALUES ('" ;
  405. insert += this.textBox1.Text + "', '" ;
  406. insert += this.textBox2.Text + "', '" ;
  407. insert += this.textBox3.Text + "','" ;
  408. insert += this.textBox4.Text + "') " ;
  409. //初始化OleDbCommand
  410. OleDbCommand comm=new OleDbCommand();
  411. comm.CommandText=insert;
  412. comm.Connection=this.oleDbConnection1;
  413. //打开数据库连接
  414.                     this.oleDbConnection1.Open();
  415. //执行添加记录的语句
  416. comm.ExecuteNonQuery();
  417.                     //关闭记录
  418. this.oleDbConnection1.Close();
  419. //
  420. // //更新数据集
  421. // this.dataSet1.Tables["information"].Rows[this.myBind.Position].BeginEdit();
  422. // this.dataSet1.Tables["information"].Rows[this.myBind.Position].EndEdit();
  423. // this.dataSet1.AcceptChanges();
  424. //
  425. // //成功提示
  426. // MessageBox.Show("添加记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  427. //                    this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
  428. }
  429. else
  430. {
  431. //错误提示
  432. MessageBox.Show("请将字段信息输入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  433. }
  434. }
  435. catch (Exception error)
  436. {
  437. //错误提示 
  438. MessageBox.Show("添加记录发生错误:"+error.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  439.              //如果打开了连接,则关闭
  440. if (this.oleDbConnection1.State==ConnectionState.Open)
  441.   {
  442.     this.oleDbConnection1.Close();
  443.   }
  444. }
  445. }
  446. private void button6_Click(object sender, System.EventArgs e)
  447. {
  448. //确认删除记录
  449. if (MessageBox.Show("是否删除记录","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK)
  450. {
  451. try
  452. {
  453.     //删除记录时的SQL语句
  454.     string delete= " DELETE From information WHERE 姓名='" ;
  455. delete += this.textBox1.Text + "'" ;
  456.         //初始化OleDbCommand
  457.     OleDbCommand comm=new OleDbCommand();
  458. comm.CommandText=delete;
  459. comm.Connection=this.oleDbConnection1;
  460.     //打开数据库连接
  461. this.oleDbConnection1.Open();
  462.     //执行删除语句
  463. comm.ExecuteNonQuery();
  464.     //关闭数据库连接
  465. this.oleDbConnection1.Close();
  466.    
  467.     //更新数据集
  468. this.dataSet1.Tables["information"].Rows[this.myBind.Position].Delete();
  469. this.dataSet1.AcceptChanges();
  470.     //成功提示
  471.     MessageBox.Show("删除记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  472.                         this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
  473. }
  474. catch (Exception error)
  475. {
  476. //错误提示
  477. MessageBox.Show("删除记录发生错误:"+error.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  478. //如果打开了连接,则关闭
  479. if (this.oleDbConnection1.State==ConnectionState.Open)
  480. {
  481. this.oleDbConnection1.Close();
  482. }
  483. }
  484.   }
  485. }
  486. private void button7_Click(object sender, System.EventArgs e)
  487. {
  488. //请空数据,准备编辑
  489. this.textBox1.Text="";
  490. this.textBox2.Text="";
  491. this.textBox3.Text="";
  492. this.textBox4.Text="";
  493. }
  494. private void button8_Click(object sender, System.EventArgs e)
  495. {
  496. try
  497. {
  498. //首先判断添加的信息是否完整
  499. if (this.textBox1.Text!="" & this.textBox2.Text!="" & this.textBox3.Text!="" & this.textBox4.Text!="")
  500. {
  501. //更新记录的SQL语句
  502. string update= " UPDATE information SET ";
  503. update +=  " 年龄='" ;
  504. update += this.textBox2.Text + "', 电话='" ;
  505. update += this.textBox3.Text + "', 电子邮件='" ;
  506. update += this.textBox4.Text + "' WHERE 姓名='" ;
  507. update += this.textBox1.Text + "'";
  508. //初始化OleDbCommand对象
  509. OleDbCommand comm=new OleDbCommand();
  510. comm.CommandText=update;
  511. comm.Connection=this.oleDbConnection1;
  512. //打开数据库连接
  513. this.oleDbConnection1.Open();
  514. //执行更新语句
  515. comm.ExecuteNonQuery();
  516. //关闭数据库连接
  517. this.oleDbConnection1.Close();
  518. //更新数据集
  519. this.dataSet1.Tables["information"].Rows[this.myBind.Position].BeginEdit();
  520. this.dataSet1.Tables["information"].Rows[this.myBind.Position].EndEdit();
  521. this.dataSet1.AcceptChanges();
  522. //成功提示
  523. MessageBox.Show("修改记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  524. this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
  525. }
  526. else
  527. {
  528. //错误提示
  529. MessageBox.Show("请将字段信息输入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  530. }
  531. }
  532. catch (Exception error)
  533. {
  534. //错误提示
  535. MessageBox.Show("修改记录发生错误:"+error.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  536. //如果打开了连接,则关闭
  537. if (this.oleDbConnection1.State==ConnectionState.Open)
  538. {
  539. this.oleDbConnection1.Close();
  540. }
  541. }
  542. }
  543. }
  544. }