Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 17k
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. using System.Xml;
  9. namespace ch6_5
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
  17. private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
  18. private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
  19. private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
  20. private System.Data.OleDb.OleDbConnection oleDbConnection1;
  21. private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
  22. private System.Windows.Forms.Button button1;
  23. private System.Windows.Forms.DataGrid dataGrid1;
  24. private ch6_5.DataSet1 dataSet11;
  25. private System.Windows.Forms.Button button2;
  26. private System.Windows.Forms.Button button3;
  27. private System.Windows.Forms.Button button4;
  28. private System.Windows.Forms.Button button5;
  29. private System.Windows.Forms.Splitter splitter1;
  30. private System.Windows.Forms.RichTextBox richTextBox1;
  31. /// <summary>
  32. /// 必需的设计器变量。
  33. /// </summary>
  34. private System.ComponentModel.Container components = null;
  35. public Form1()
  36. {
  37. //
  38. // Windows 窗体设计器支持所必需的
  39. //
  40. InitializeComponent();
  41. //
  42. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  43. //
  44. }
  45. /// <summary>
  46. /// 清理所有正在使用的资源。
  47. /// </summary>
  48. protected override void Dispose( bool disposing )
  49. {
  50. if( disposing )
  51. {
  52. if (components != null) 
  53. {
  54. components.Dispose();
  55. }
  56. }
  57. base.Dispose( disposing );
  58. }
  59. #region Windows Form Designer generated code
  60. /// <summary>
  61. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  62. /// 此方法的内容。
  63. /// </summary>
  64. private void InitializeComponent()
  65. {
  66. this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
  67. this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
  68. this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
  69. this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
  70. this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
  71. this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
  72. this.button1 = new System.Windows.Forms.Button();
  73. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  74. this.dataSet11 = new ch6_5.DataSet1();
  75. this.button2 = new System.Windows.Forms.Button();
  76. this.button3 = new System.Windows.Forms.Button();
  77. this.button4 = new System.Windows.Forms.Button();
  78. this.button5 = new System.Windows.Forms.Button();
  79. this.splitter1 = new System.Windows.Forms.Splitter();
  80. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  81. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  82. ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
  83. this.SuspendLayout();
  84. // 
  85. // oleDbSelectCommand1
  86. // 
  87. this.oleDbSelectCommand1.CommandText = "SELECT 电话, 电子邮件, 年龄, 姓名 FROM information";
  88. this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
  89. // 
  90. // oleDbConnection1
  91. // 
  92. 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";
  93. // 
  94. // oleDbInsertCommand1
  95. // 
  96. this.oleDbInsertCommand1.CommandText = "INSERT INTO information(电话, 电子邮件, 年龄, 姓名) VALUES (?, ?, ?, ?)";
  97. this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
  98. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 50, "电话"));
  99. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, "电子邮件"));
  100. 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));
  101. this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("姓名", System.Data.OleDb.OleDbType.VarWChar, 50, "姓名"));
  102. // 
  103. // oleDbUpdateCommand1
  104. // 
  105. this.oleDbUpdateCommand1.CommandText = "UPDATE information SET 电话 = ?, 电子邮件 = ?, 年龄 = ?, 姓名 = ? WHERE (姓名 = ?) AND (电子邮件 " +
  106. "= ? OR ? IS NULL AND 电子邮件 IS NULL) AND (电话 = ? OR ? IS NULL AND 电话 IS NULL)";
  107. this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
  108. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电话", System.Data.OleDb.OleDbType.VarWChar, 50, "电话"));
  109. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("电子邮件", System.Data.OleDb.OleDbType.VarWChar, 50, "电子邮件"));
  110. 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));
  111. this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("姓名", System.Data.OleDb.OleDbType.VarWChar, 50, "姓名"));
  112. 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));
  113. 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));
  114. 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));
  115. 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));
  116. 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));
  117. // 
  118. // oleDbDeleteCommand1
  119. // 
  120. this.oleDbDeleteCommand1.CommandText = "DELETE FROM information WHERE (姓名 = ?) AND (电子邮件 = ? OR ? IS NULL AND 电子邮件 IS NUL" +
  121. "L) AND (电话 = ? OR ? IS NULL AND 电话 IS NULL)";
  122. this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
  123. 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));
  124. 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));
  125. 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));
  126. 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));
  127. 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));
  128. // 
  129. // oleDbDataAdapter1
  130. // 
  131. this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
  132. this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
  133. this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
  134. this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
  135. new System.Data.Common.DataTableMapping("Table", "information", new System.Data.Common.DataColumnMapping[] {
  136.    new System.Data.Common.DataColumnMapping("姓名", "姓名"),
  137.    new System.Data.Common.DataColumnMapping("年龄", "年龄"),
  138.    new System.Data.Common.DataColumnMapping("电话", "电话"),
  139.    new System.Data.Common.DataColumnMapping("电子邮件", "电子邮件")})});
  140. this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
  141. // 
  142. // button1
  143. // 
  144. this.button1.Location = new System.Drawing.Point(0, 336);
  145. this.button1.Name = "button1";
  146. this.button1.TabIndex = 1;
  147. this.button1.Text = "读出数据";
  148. this.button1.Click += new System.EventHandler(this.button1_Click);
  149. // 
  150. // dataGrid1
  151. // 
  152. this.dataGrid1.AlternatingBackColor = System.Drawing.Color.OldLace;
  153. this.dataGrid1.BackColor = System.Drawing.Color.OldLace;
  154. this.dataGrid1.BackgroundColor = System.Drawing.Color.Tan;
  155. this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
  156. this.dataGrid1.CaptionBackColor = System.Drawing.Color.SaddleBrown;
  157. this.dataGrid1.CaptionForeColor = System.Drawing.Color.OldLace;
  158. this.dataGrid1.DataMember = "";
  159. this.dataGrid1.DataSource = this.dataSet11.information;
  160. this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
  161. this.dataGrid1.FlatMode = true;
  162. this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
  163. this.dataGrid1.ForeColor = System.Drawing.Color.DarkSlateGray;
  164. this.dataGrid1.GridLineColor = System.Drawing.Color.Tan;
  165. this.dataGrid1.HeaderBackColor = System.Drawing.Color.Wheat;
  166. this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
  167. this.dataGrid1.HeaderForeColor = System.Drawing.Color.SaddleBrown;
  168. this.dataGrid1.LinkColor = System.Drawing.Color.DarkSlateBlue;
  169. this.dataGrid1.Name = "dataGrid1";
  170. this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.OldLace;
  171. this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.DarkSlateGray;
  172. this.dataGrid1.SelectionBackColor = System.Drawing.Color.SlateGray;
  173. this.dataGrid1.SelectionForeColor = System.Drawing.Color.White;
  174. this.dataGrid1.Size = new System.Drawing.Size(504, 168);
  175. this.dataGrid1.TabIndex = 2;
  176. // 
  177. // dataSet11
  178. // 
  179. this.dataSet11.DataSetName = "DataSet1";
  180. this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
  181. this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
  182. // 
  183. // button2
  184. // 
  185. this.button2.Location = new System.Drawing.Point(80, 336);
  186. this.button2.Name = "button2";
  187. this.button2.TabIndex = 4;
  188. this.button2.Text = "读出XML";
  189. this.button2.Click += new System.EventHandler(this.button2_Click);
  190. // 
  191. // button3
  192. // 
  193. this.button3.Location = new System.Drawing.Point(160, 336);
  194. this.button3.Name = "button3";
  195. this.button3.Size = new System.Drawing.Size(128, 23);
  196. this.button3.TabIndex = 5;
  197. this.button3.Text = "读出XML Schema";
  198. this.button3.Click += new System.EventHandler(this.button3_Click);
  199. // 
  200. // button4
  201. // 
  202. this.button4.Location = new System.Drawing.Point(296, 336);
  203. this.button4.Name = "button4";
  204. this.button4.Size = new System.Drawing.Size(80, 23);
  205. this.button4.TabIndex = 6;
  206. this.button4.Text = "写入XML文件";
  207. this.button4.Click += new System.EventHandler(this.button4_Click);
  208. // 
  209. // button5
  210. // 
  211. this.button5.Location = new System.Drawing.Point(384, 336);
  212. this.button5.Name = "button5";
  213. this.button5.Size = new System.Drawing.Size(112, 23);
  214. this.button5.TabIndex = 7;
  215. this.button5.Text = "从XML文件中载入";
  216. this.button5.Click += new System.EventHandler(this.button5_Click);
  217. // 
  218. // splitter1
  219. // 
  220. this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
  221. this.splitter1.Location = new System.Drawing.Point(0, 168);
  222. this.splitter1.Name = "splitter1";
  223. this.splitter1.Size = new System.Drawing.Size(504, 3);
  224. this.splitter1.TabIndex = 8;
  225. this.splitter1.TabStop = false;
  226. // 
  227. // richTextBox1
  228. // 
  229. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
  230. this.richTextBox1.Location = new System.Drawing.Point(0, 171);
  231. this.richTextBox1.Name = "richTextBox1";
  232. this.richTextBox1.Size = new System.Drawing.Size(504, 149);
  233. this.richTextBox1.TabIndex = 9;
  234. this.richTextBox1.Text = "";
  235. // 
  236. // Form1
  237. // 
  238. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  239. this.ClientSize = new System.Drawing.Size(504, 365);
  240. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  241.   this.richTextBox1,
  242.   this.splitter1,
  243.   this.button5,
  244.   this.button4,
  245.   this.button3,
  246.   this.button2,
  247.   this.dataGrid1,
  248.   this.button1});
  249. this.Name = "Form1";
  250. this.Text = "DataSet与XML";
  251. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  252. ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
  253. this.ResumeLayout(false);
  254. }
  255. #endregion
  256. /// <summary>
  257. /// 应用程序的主入口点。
  258. /// </summary>
  259. [STAThread]
  260. static void Main() 
  261. {
  262. Application.Run(new Form1());
  263. }
  264. private void button1_Click(object sender, System.EventArgs e)
  265. {
  266. this.richTextBox1.Text="";
  267.     //打开数据链接
  268. this.oleDbConnection1.Open();
  269. //在dataGrid中载入数据
  270. this.oleDbDataAdapter1.Fill(this.dataSet11,"information");
  271.             //代表数据集内容
  272.     string s="";
  273. //得到OleDbDataReader对象
  274. OleDbDataReader dReader=this.oleDbDataAdapter1.SelectCommand.ExecuteReader();
  275. //读去数据源
  276. while (dReader.Read())
  277. {
  278. //读出姓名
  279. s+=dReader.GetString(3)+"n";
  280. //读出电话
  281. s+=dReader.GetString(0)+"n";
  282. //读出电子邮件
  283. s+=dReader.GetString(1)+"n";
  284. //读出年龄
  285. s+=dReader.GetInt32(2).ToString()+"n";
  286. }
  287. this.richTextBox1.Text=s;
  288. //关闭数据连接
  289. this.oleDbConnection1.Close();
  290. }
  291. private void button2_Click(object sender, System.EventArgs e)
  292. {
  293. this.richTextBox1.Text="";
  294. //打开连接
  295. this.oleDbConnection1.Open();
  296. //填充数据集
  297. this.oleDbDataAdapter1.Fill(this.dataSet11,"information");
  298. //关闭连接
  299. this.oleDbConnection1.Close();
  300. //返回数据集的XML表示形式
  301. string s=this.dataSet11.GetXml();
  302. this.richTextBox1.Text=s;
  303. }
  304. private void button3_Click(object sender, System.EventArgs e)
  305. {
  306.    
  307. this.richTextBox1.Text="";
  308. this.oleDbConnection1.Open();
  309. this.oleDbDataAdapter1.Fill(this.dataSet11,"information");
  310. this.oleDbConnection1.Close();
  311. //返回数据的XML表示形式的XSD架构
  312. string s=this.dataSet11.GetXmlSchema();
  313. this.richTextBox1.Text=s;
  314. }
  315. private void button4_Click(object sender, System.EventArgs e)
  316. {
  317. this.oleDbConnection1.Open();
  318. this.oleDbDataAdapter1.Fill(this.dataSet11,"information");
  319. this.oleDbConnection1.Close();
  320. //将数据写入Xml文件
  321. this.dataSet11.WriteXml("D:\information.xml",XmlWriteMode.WriteSchema);
  322. }
  323. private void button5_Click(object sender, System.EventArgs e)
  324. {
  325.    //创建XmlTextReader对象
  326.    XmlTextReader xReader= new XmlTextReader("D:\information.xml");
  327.    //创建一个新的数据集
  328.    DataSet  ds=new DataSet();
  329.    //从xReader中读出数据集
  330.    ds.ReadXml(xReader,XmlReadMode.ReadSchema);
  331.    //在dataGrid中显示新的数据集
  332.    this.dataGrid1.DataSource=ds;  
  333. }
  334. }
  335. }