tabledisplay.cs

来自「this is a good book for the visual c#」· CS 代码 · 共 196 行

CS
196
字号
// Fig. 19.27: TableDisplay.cs
// Displays data from a database table.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TableDisplay
{
   /// <summary>
   /// Summary description for TableDisplay.cs.
   /// </summary>
   public class TableDisplay : System.Windows.Forms.Form
   {
      private System.Data.DataSet dataSet1;
      private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
      private System.Windows.Forms.DataGrid dataGrid1;
      private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
      private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
      private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
      private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
      private System.Data.OleDb.OleDbConnection oleDbConnection1;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      public TableDisplay()
      {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
         
         // Fill dataSet1 with data
         oleDbDataAdapter1.Fill( dataSet1, "Authors" );

         // Bind data in Authors table in dataSet1 to dataGrid1
         dataGrid1.SetDataBinding( dataSet1, "Authors" );
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
         if( disposing )
         {
            if (components != null) 
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

		#region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
          this.dataSet1 = new System.Data.DataSet();
          this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
          this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
          this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
          this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
          this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
          this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
          this.dataGrid1 = new System.Windows.Forms.DataGrid();
          ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
          ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
          this.SuspendLayout();
          // 
          // dataSet1
          // 
          this.dataSet1.DataSetName = "NewDataSet";
          this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");
          // 
          // oleDbDataAdapter1
          // 
          this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
          this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
          this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
          this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
            new System.Data.Common.DataTableMapping("Table", "Authors", new System.Data.Common.DataColumnMapping[] {
                        new System.Data.Common.DataColumnMapping("authorID", "authorID"),
                        new System.Data.Common.DataColumnMapping("firstName", "firstName"),
                        new System.Data.Common.DataColumnMapping("lastName", "lastName")})});
          this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
          // 
          // oleDbDeleteCommand1
          // 
          this.oleDbDeleteCommand1.CommandText = "DELETE FROM Authors WHERE (authorID = ?) AND (firstName = ? OR ? IS NULL AND firs" +
              "tName IS NULL) AND (lastName = ? OR ? IS NULL AND lastName IS NULL)";
          this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
          this.oleDbDeleteCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
            new System.Data.OleDb.OleDbParameter("Original_authorID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((byte)(10)), ((byte)(0)), "authorID", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_firstName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "firstName", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_firstName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "firstName", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_lastName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "lastName", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_lastName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "lastName", System.Data.DataRowVersion.Original, null)});
          // 
          // oleDbConnection1
          // 
          this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Users\\TA\\Desktop\\flash\\C#\\C# How" +
              " To Program - & Examples - Deitel\\Examples\\ch19\\Fig19_29\\AddressBook\\AddressBook" +
              ".mdb\"";
          // 
          // oleDbInsertCommand1
          // 
          this.oleDbInsertCommand1.CommandText = "INSERT INTO Authors(firstName, lastName) VALUES (?, ?)";
          this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
          this.oleDbInsertCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
            new System.Data.OleDb.OleDbParameter("firstName", System.Data.OleDb.OleDbType.VarWChar, 50, "firstName"),
            new System.Data.OleDb.OleDbParameter("lastName", System.Data.OleDb.OleDbType.VarWChar, 50, "lastName")});
          // 
          // oleDbSelectCommand1
          // 
          this.oleDbSelectCommand1.CommandText = "SELECT authorID, firstName, lastName FROM Authors";
          this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
          // 
          // oleDbUpdateCommand1
          // 
          this.oleDbUpdateCommand1.CommandText = "UPDATE Authors SET firstName = ?, lastName = ? WHERE (authorID = ?) AND (firstNam" +
              "e = ? OR ? IS NULL AND firstName IS NULL) AND (lastName = ? OR ? IS NULL AND las" +
              "tName IS NULL)";
          this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
          this.oleDbUpdateCommand1.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
            new System.Data.OleDb.OleDbParameter("firstName", System.Data.OleDb.OleDbType.VarWChar, 50, "firstName"),
            new System.Data.OleDb.OleDbParameter("lastName", System.Data.OleDb.OleDbType.VarWChar, 50, "lastName"),
            new System.Data.OleDb.OleDbParameter("Original_authorID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((byte)(10)), ((byte)(0)), "authorID", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_firstName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "firstName", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_firstName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "firstName", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_lastName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "lastName", System.Data.DataRowVersion.Original, null),
            new System.Data.OleDb.OleDbParameter("Original_lastName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "lastName", System.Data.DataRowVersion.Original, null)});
          // 
          // dataGrid1
          // 
          this.dataGrid1.DataMember = "";
          this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
          this.dataGrid1.Location = new System.Drawing.Point(16, 16);
          this.dataGrid1.Name = "dataGrid1";
          this.dataGrid1.Size = new System.Drawing.Size(264, 248);
          this.dataGrid1.TabIndex = 0;
          // 
          // TableDisplay
          // 
          this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
          this.ClientSize = new System.Drawing.Size(292, 273);
          this.Controls.Add(this.dataGrid1);
          this.Name = "TableDisplay";
          this.Text = "TableDisplay";
          ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
          ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
          this.ResumeLayout(false);

      }   // end of InitializeComponent
		#endregion

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main() 
      {
         Application.Run(new TableDisplay());
      }
   }
}

/*
 **************************************************************************
 * (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall.     *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?