⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-14-06

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
📖 第 1 页 / 共 2 页
字号:
// Example 14-06: Updating, deleting, and adding records
// Updated 08-15-2001 Jesse Liberty to add explicit command objects

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace ProgrammingCSharpWindows.Form
{
   public class ADOForm1 : System.Windows.Forms.Form
   {
      private System.ComponentModel.Container components;
      private System.Windows.Forms.Label label9;
      private System.Windows.Forms.TextBox txtPhone;
      private System.Windows.Forms.Label label8;
      private System.Windows.Forms.TextBox txtContactTitle;
      private System.Windows.Forms.Label label7;
      private System.Windows.Forms.TextBox txtZip;
      private System.Windows.Forms.Label label6;
      private System.Windows.Forms.TextBox txtCity;
      private System.Windows.Forms.Label label5;
      private System.Windows.Forms.TextBox txtAddress;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.TextBox txtContactName;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.TextBox txtCompanyName;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.TextBox txtCompanyID;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Button btnNew;
      private System.Windows.Forms.TextBox txtCustomerName;
      private System.Windows.Forms.Button btnUpdate;
      private System.Windows.Forms.Label lblMessage;
      private System.Windows.Forms.Button btnDelete;
      private System.Windows.Forms.ListBox lbCustomers;
      private SqlDataAdapter DataAdapter;

      // the DataSet and table are members so that
      // we can access them from any member method
      private DataSet DataSet;
      private DataTable dataTable;

      public ADOForm1()
      {
         InitializeComponent();

         string connectionString = 
            "server=YourServer; uid=sa; pwd=yourPasswd; database=northwind";
         string commandString = "Select * from Customers";



         DataAdapter = 
            new SqlDataAdapter(commandString, connectionString);
         DataSet = new DataSet();
         DataAdapter.Fill(DataSet,"Customers");
         PopulateLB();
      }

      // fill the list box with columns from the Customers table
      private void PopulateLB()
      {
         dataTable = DataSet.Tables[0];
         lbCustomers.Items.Clear();
         foreach (DataRow dataRow in dataTable.Rows)
         {
            lbCustomers.Items.Add(
               dataRow["CompanyName"] + " (" + 
               dataRow["ContactName"] + ")" );
         }
            
      }

      public override void Dispose()
      {
         base.Dispose();
         components.Dispose();
      }

      private void InitializeComponent()
      {
         this.components = new System.ComponentModel.Container ();
         this.txtCustomerName = new System.Windows.Forms.TextBox ();
         this.txtCity = new System.Windows.Forms.TextBox ();
         this.txtCompanyID = new System.Windows.Forms.TextBox ();
         this.lblMessage = new System.Windows.Forms.Label ();
         this.btnUpdate = new System.Windows.Forms.Button ();
         this.txtContactName = new System.Windows.Forms.TextBox ();
         this.txtZip = new System.Windows.Forms.TextBox ();
         this.btnDelete = new System.Windows.Forms.Button ();
         this.txtContactTitle = new System.Windows.Forms.TextBox ();
         this.txtAddress = new System.Windows.Forms.TextBox ();
         this.txtCompanyName = new System.Windows.Forms.TextBox ();
         this.label5 = new System.Windows.Forms.Label ();
         this.label6 = new System.Windows.Forms.Label ();
         this.label7 = new System.Windows.Forms.Label ();
         this.label8 = new System.Windows.Forms.Label ();
         this.label9 = new System.Windows.Forms.Label ();
         this.label4 = new System.Windows.Forms.Label ();
         this.lbCustomers = new System.Windows.Forms.ListBox ();
         this.txtPhone = new System.Windows.Forms.TextBox ();
         this.btnNew = new System.Windows.Forms.Button ();
         this.label1 = new System.Windows.Forms.Label ();
         this.label2 = new System.Windows.Forms.Label ();
         this.label3 = new System.Windows.Forms.Label ();
         //@this.TrayHeight = 0;
         //@this.TrayLargeIcon = false;
         //@this.TrayAutoArrange = true;
         txtCustomerName.Location = new System.Drawing.Point (256, 120);
         txtCustomerName.TabIndex = 4;
         txtCustomerName.Size = new System.Drawing.Size (160, 20);
         txtCity.Location = new System.Drawing.Point (384, 245);
         txtCity.TabIndex = 15;
         txtCity.Size = new System.Drawing.Size (160, 20);
         txtCompanyID.Location = new System.Drawing.Point (136, 216);
         txtCompanyID.TabIndex = 7;
         txtCompanyID.Size = new System.Drawing.Size (160, 20);
         lblMessage.Location = new System.Drawing.Point (32, 368);
         lblMessage.Text = "Press New, Update or Delete";
         lblMessage.Size = new System.Drawing.Size (416, 48);
         lblMessage.TabIndex = 1;
         btnUpdate.Location = new System.Drawing.Point (32, 120);
         btnUpdate.Size = new System.Drawing.Size (75, 23);
         btnUpdate.TabIndex = 0;
         btnUpdate.Text = "Update";
         btnUpdate.Click += 
            new System.EventHandler (this.btnUpdate_Click);
         txtContactName.Location = new System.Drawing.Point (136, 274);
         txtContactName.TabIndex = 11;
         txtContactName.Size = new System.Drawing.Size (160, 20);
         txtZip.Location = new System.Drawing.Point (384, 274);
         txtZip.TabIndex = 17;
         txtZip.Size = new System.Drawing.Size (160, 20);
         btnDelete.Location = new System.Drawing.Point (472, 120);
         btnDelete.Size = new System.Drawing.Size (75, 23);
         btnDelete.TabIndex = 2;
         btnDelete.Text = "Delete";
         btnDelete.Click += 
            new System.EventHandler (this.btnDelete_Click);
         txtContactTitle.Location = new System.Drawing.Point (136, 303);
         txtContactTitle.TabIndex = 19;
         txtContactTitle.Size = new System.Drawing.Size (160, 20);
         txtAddress.Location = new System.Drawing.Point (384, 216);
         txtAddress.TabIndex = 13;
         txtAddress.Size = new System.Drawing.Size (160, 20);
         txtCompanyName.Location = new System.Drawing.Point (136, 245);
         txtCompanyName.TabIndex = 9;
         txtCompanyName.Size = new System.Drawing.Size (160, 20);
         label5.Location = new System.Drawing.Point (320, 252);
         label5.Text = "City";
         label5.Size = new System.Drawing.Size (48, 16);
         label5.TabIndex = 14;
         label6.Location = new System.Drawing.Point (320, 284);
         label6.Text = "Zip";
         label6.Size = new System.Drawing.Size (40, 16);
         label6.TabIndex = 16;
         label7.Location = new System.Drawing.Point (40, 312);
         label7.Text = "Contact Title";
         label7.Size = new System.Drawing.Size (88, 16);
         label7.TabIndex = 18;
         label8.Location = new System.Drawing.Point (320, 312);
         label8.Text = "Phone";
         label8.Size = new System.Drawing.Size (56, 16);
         label8.TabIndex = 20;
         label9.Location = new System.Drawing.Point (120, 120);
         label9.Text = "New Customer Name:";
         label9.Size = new System.Drawing.Size (120, 24);
         label9.TabIndex = 22;
         label4.Location = new System.Drawing.Point (320, 224);
         label4.Text = "Address";
         label4.Size = new System.Drawing.Size (56, 16);
         label4.TabIndex = 12;
         lbCustomers.Location = new System.Drawing.Point (32, 16);
         lbCustomers.Size = new System.Drawing.Size (512, 95);
         lbCustomers.TabIndex = 3;
         txtPhone.Location = new System.Drawing.Point (384, 303);
         txtPhone.TabIndex = 21;
         txtPhone.Size = new System.Drawing.Size (160, 20);
         btnNew.Location = new System.Drawing.Point (472, 336);
         btnNew.Size = new System.Drawing.Size (75, 23);
         btnNew.TabIndex = 5;
         btnNew.Text = "New";
         btnNew.Click += new System.EventHandler (this.btnNew_Click);
         label1.Location = new System.Drawing.Point (40, 224);
         label1.Text = "Company ID";
         label1.Size = new System.Drawing.Size (88, 16);
         label1.TabIndex = 6;
         label2.Location = new System.Drawing.Point (40, 252);
         label2.Text = "Company Name";
         label2.Size = new System.Drawing.Size (88, 16);
         label2.TabIndex = 8;
         label3.Location = new System.Drawing.Point (40, 284);
         label3.Text = "Contact Name";
         label3.Size = new System.Drawing.Size (88, 16);
         label3.TabIndex = 10;
         this.Text = "Customers Update Form";
         this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
         this.ClientSize = new System.Drawing.Size (584, 421);
         this.Controls.Add (this.label9);
         this.Controls.Add (this.txtPhone);
         this.Controls.Add (this.label8);
         this.Controls.Add (this.txtContactTitle);
         this.Controls.Add (this.label7);
         this.Controls.Add (this.txtZip);
         this.Controls.Add (this.label6);
         this.Controls.Add (this.txtCity);
         this.Controls.Add (this.label5);
         this.Controls.Add (this.txtAddress);
         this.Controls.Add (this.label4);
         this.Controls.Add (this.txtContactName);
         this.Controls.Add (this.label3);
         this.Controls.Add (this.txtCompanyName);
         this.Controls.Add (this.label2);

⌨️ 快捷键说明

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