Form1.cs
上传用户:chinafred
上传日期:2007-08-14
资源大小:10127k
文件大小:17k
源码类别:

数据库编程

开发平台:

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_1
  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.Windows.Forms.GroupBox groupBox1;
  17. private System.Windows.Forms.Label label1;
  18. private System.Windows.Forms.Label label2;
  19. private System.Windows.Forms.Label label3;
  20. private System.Windows.Forms.Label label4;
  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.Button button1;
  26. private System.Windows.Forms.Button button2;
  27. private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
  28. private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
  29. private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
  30. private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
  31. private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
  32. private System.Data.DataSet dataSet1;
  33. private BindingManagerBase myBind;
  34. private System.Windows.Forms.Button button3;
  35. private System.Windows.Forms.Button button4;
  36. /// <summary>
  37. /// 必需的设计器变量。
  38. /// </summary>
  39. private System.ComponentModel.Container components = null;
  40. public Form1()
  41. {
  42. //
  43. // Windows 窗体设计器支持所必需的
  44. //
  45. InitializeComponent();
  46. DataBinding();
  47. //
  48. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  49. //
  50. }
  51. private void DataBinding()
  52.     {
  53.             //刷新数据集
  54. this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
  55.             //数据绑定
  56. this.textBox1.DataBindings.Add("Text",this.dataSet1,"information.姓名");
  57. this.textBox2.DataBindings.Add("Text",this.dataSet1,"information.年龄");
  58. this.textBox3.DataBindings.Add("Text",this.dataSet1,"information.电话");
  59. this.textBox4.DataBindings.Add("Text",this.dataSet1,"information.电子邮件");
  60. //构造
  61. myBind=this.BindingContext[this.dataSet1,"information"];
  62.    
  63. }
  64. /// <summary>
  65. /// 清理所有正在使用的资源。
  66. /// </summary>
  67. protected override void Dispose( bool disposing )
  68. {
  69. if( disposing )
  70. {
  71. if (components != null) 
  72. {
  73. components.Dispose();
  74. }
  75. }
  76. base.Dispose( disposing );
  77. }
  78. #region Windows Form Designer generated code
  79. /// <summary>
  80. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  81. /// 此方法的内容。
  82. /// </summary>
  83. private void InitializeComponent()
  84. {
  85. this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
  86. this.groupBox1 = new System.Windows.Forms.GroupBox();
  87. this.textBox4 = new System.Windows.Forms.TextBox();
  88. this.textBox3 = new System.Windows.Forms.TextBox();
  89. this.textBox2 = new System.Windows.Forms.TextBox();
  90. this.textBox1 = new System.Windows.Forms.TextBox();
  91. this.label4 = new System.Windows.Forms.Label();
  92. this.label3 = new System.Windows.Forms.Label();
  93. this.label2 = new System.Windows.Forms.Label();
  94. this.label1 = new System.Windows.Forms.Label();
  95. this.button1 = new System.Windows.Forms.Button();
  96. this.button2 = new System.Windows.Forms.Button();
  97. this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
  98. this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
  99. this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
  100. this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
  101. this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
  102. this.dataSet1 = new System.Data.DataSet();
  103. this.button3 = new System.Windows.Forms.Button();
  104. this.button4 = new System.Windows.Forms.Button();
  105. this.groupBox1.SuspendLayout();
  106. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
  107. this.SuspendLayout();
  108. // 
  109. // oleDbConnection1
  110. // 
  111. 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";
  112. // 
  113. // groupBox1
  114. // 
  115. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  116. this.textBox4,
  117. this.textBox3,
  118. this.textBox2,
  119. this.textBox1,
  120. this.label4,
  121. this.label3,
  122. this.label2,
  123. this.label1});
  124. this.groupBox1.Location = new System.Drawing.Point(16, 16);
  125. this.groupBox1.Name = "groupBox1";
  126. this.groupBox1.Size = new System.Drawing.Size(264, 168);
  127. this.groupBox1.TabIndex = 1;
  128. this.groupBox1.TabStop = false;
  129. this.groupBox1.Text = "我的朋友";
  130. // 
  131. // textBox4
  132. // 
  133. this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  134. this.textBox4.Location = new System.Drawing.Point(96, 124);
  135. this.textBox4.Name = "textBox4";
  136. this.textBox4.Size = new System.Drawing.Size(152, 21);
  137. this.textBox4.TabIndex = 8;
  138. this.textBox4.Text = "";
  139. // 
  140. // textBox3
  141. // 
  142. this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  143. this.textBox3.Location = new System.Drawing.Point(96, 92);
  144. this.textBox3.Name = "textBox3";
  145. this.textBox3.Size = new System.Drawing.Size(152, 21);
  146. this.textBox3.TabIndex = 7;
  147. this.textBox3.Text = "";
  148. // 
  149. // textBox2
  150. // 
  151. this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  152. this.textBox2.Location = new System.Drawing.Point(96, 58);
  153. this.textBox2.Name = "textBox2";
  154. this.textBox2.Size = new System.Drawing.Size(152, 21);
  155. this.textBox2.TabIndex = 6;
  156. this.textBox2.Text = "";
  157. // 
  158. // textBox1
  159. // 
  160. this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  161. this.textBox1.Location = new System.Drawing.Point(96, 26);
  162. this.textBox1.Name = "textBox1";
  163. this.textBox1.Size = new System.Drawing.Size(152, 21);
  164. this.textBox1.TabIndex = 5;
  165. this.textBox1.Text = "";
  166. // 
  167. // label4
  168. // 
  169. this.label4.Location = new System.Drawing.Point(16, 128);
  170. this.label4.Name = "label4";
  171. this.label4.Size = new System.Drawing.Size(64, 16);
  172. this.label4.TabIndex = 4;
  173. this.label4.Text = "电子邮件";
  174. // 
  175. // label3
  176. // 
  177. this.label3.Location = new System.Drawing.Point(16, 96);
  178. this.label3.Name = "label3";
  179. this.label3.Size = new System.Drawing.Size(72, 16);
  180. this.label3.TabIndex = 3;
  181. this.label3.Text = "电话";
  182. // 
  183. // label2
  184. // 
  185. this.label2.Location = new System.Drawing.Point(16, 64);
  186. this.label2.Name = "label2";
  187. this.label2.Size = new System.Drawing.Size(72, 16);
  188. this.label2.TabIndex = 2;
  189. this.label2.Text = "年龄";
  190. // 
  191. // label1
  192. // 
  193. this.label1.Location = new System.Drawing.Point(16, 32);
  194. this.label1.Name = "label1";
  195. this.label1.Size = new System.Drawing.Size(72, 16);
  196. this.label1.TabIndex = 1;
  197. this.label1.Text = "姓名";
  198. // 
  199. // button1
  200. // 
  201. this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  202. this.button1.Location = new System.Drawing.Point(72, 200);
  203. this.button1.Name = "button1";
  204. this.button1.Size = new System.Drawing.Size(72, 24);
  205. this.button1.TabIndex = 2;
  206. this.button1.Text = "前一个";
  207. this.button1.Click += new System.EventHandler(this.button1_Click);
  208. // 
  209. // button2
  210. // 
  211. this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  212. this.button2.Location = new System.Drawing.Point(144, 200);
  213. this.button2.Name = "button2";
  214. this.button2.Size = new System.Drawing.Size(75, 24);
  215. this.button2.TabIndex = 3;
  216. this.button2.Text = "后一个";
  217. this.button2.Click += new System.EventHandler(this.button2_Click);
  218. // 
  219. // oleDbDataAdapter1
  220. // 
  221. this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
  222. this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
  223. this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
  224. this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
  225. new System.Data.Common.DataTableMapping("Table", "information", new System.Data.Common.DataColumnMapping[] {
  226.    new System.Data.Common.DataColumnMapping("姓名", "姓名"),
  227.    new System.Data.Common.DataColumnMapping("年龄", "年龄"),
  228.    new System.Data.Common.DataColumnMapping("电话", "电话"),
  229.    new System.Data.Common.DataColumnMapping("电子邮件", "电子邮件")})});
  230. this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
  231. // 
  232. // oleDbDeleteCommand1
  233. // 
  234. this.oleDbDeleteCommand1.CommandText = "DELETE FROM information WHERE (姓名 = ?) AND (电子邮件 = ? OR ? IS NULL AND 电子邮件 IS NUL" +
  235. "L) AND (电话 = ? OR ? IS NULL AND 电话 IS NULL)";
  236. this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
  237. 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));
  238. 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));
  239. 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));
  240. 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));
  241. 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));
  242. // 
  243. // oleDbInsertCommand1
  244. // 
  245. this.oleDbInsertCommand1.CommandText = "INSERT INTO information(电话, 电子邮件, 年龄, 姓名) VALUES (?, ?, ?, ?)";
  246. this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
  247. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 50, "电话"));
  248. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, "电子邮件"));
  249. 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));
  250. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("姓名", System.Data.OleDb.OleDbType.VarWChar, 50, "姓名"));
  251. // 
  252. // oleDbSelectCommand1
  253. // 
  254. this.oleDbSelectCommand1.CommandText = "SELECT 电话, 电子邮件, 年龄, 姓名 FROM information";
  255. this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
  256. // 
  257. // oleDbUpdateCommand1
  258. // 
  259. this.oleDbUpdateCommand1.CommandText = "UPDATE information SET 电话 = ?, 电子邮件 = ?, 年龄 = ?, 姓名 = ? WHERE (姓名 = ?) AND (电子邮件 " +
  260. "= ? OR ? IS NULL AND 电子邮件 IS NULL) AND (电话 = ? OR ? IS NULL AND 电话 IS NULL)";
  261. this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
  262. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 50, "电话"));
  263. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, "电子邮件"));
  264. 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));
  265. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("姓名", System.Data.OleDb.OleDbType.VarWChar, 50, "姓名"));
  266. 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));
  267. 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));
  268. 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));
  269. 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));
  270. 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));
  271. // 
  272. // dataSet1
  273. // 
  274. this.dataSet1.DataSetName = "NewDataSet";
  275. this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
  276. // 
  277. // button3
  278. // 
  279. this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  280. this.button3.Location = new System.Drawing.Point(216, 200);
  281. this.button3.Name = "button3";
  282. this.button3.Size = new System.Drawing.Size(64, 24);
  283. this.button3.TabIndex = 4;
  284. this.button3.Text = "最后";
  285. this.button3.Click += new System.EventHandler(this.button3_Click);
  286. // 
  287. // button4
  288. // 
  289. this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  290. this.button4.Location = new System.Drawing.Point(8, 200);
  291. this.button4.Name = "button4";
  292. this.button4.Size = new System.Drawing.Size(64, 24);
  293. this.button4.TabIndex = 5;
  294. this.button4.Text = "最前";
  295. this.button4.Click += new System.EventHandler(this.button4_Click);
  296. // 
  297. // Form1
  298. // 
  299. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  300. this.ClientSize = new System.Drawing.Size(292, 245);
  301. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  302.   this.button4,
  303.   this.button3,
  304.   this.button2,
  305.   this.button1,
  306.   this.groupBox1});
  307. this.Name = "Form1";
  308. this.Text = "我的通讯录";
  309. this.groupBox1.ResumeLayout(false);
  310. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
  311. this.ResumeLayout(false);
  312. }
  313. #endregion
  314. /// <summary>
  315. /// 应用程序的主入口点。
  316. /// </summary>
  317. [STAThread]
  318. static void Main() 
  319. {
  320. Application.Run(new Form1());
  321. }
  322. private void button1_Click(object sender, System.EventArgs e)
  323. {
  324. //如果不是最前一条记录,则指针向前移动
  325. if (this.myBind.Position==0)
  326. {
  327. MessageBox.Show("没有前一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information);
  328. }
  329. else
  330. {
  331. this.myBind.Position--;
  332. }
  333. }
  334. private void button2_Click(object sender, System.EventArgs e)
  335. {
  336. //如果不是最后一条记录,则指针向后移动
  337. if (this.myBind.Position==this.myBind.Count-1)
  338. {
  339. MessageBox.Show("没有后一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information);
  340. }
  341. else
  342. {
  343. this.myBind.Position++;;
  344. }
  345. }
  346. private void button3_Click(object sender, System.EventArgs e)
  347. {
  348. //指向最后一条记录
  349. this.myBind.Position=this.myBind.Count-1;
  350. }
  351. private void button4_Click(object sender, System.EventArgs e)
  352. {
  353. //指向第一条记录
  354. this.myBind.Position=0;
  355. }
  356. }
  357. }