Ado.net断开模式

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

namespace Ado.net断开模式
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.Panel panel1;
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.DataGrid dataGrid1;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;
   private System.Windows.Forms.Button button2;
   private System.Windows.Forms.Button button3;
   private System.Windows.Forms.Button button4;
   private System.Windows.Forms.Panel panel2;
   private System.Windows.Forms.LinkLabel linkLabel1;
   private System.Windows.Forms.PictureBox pictureBox1;
   private System.Windows.Forms.Label label1;

  
  
   //定义一个全局变量
   private SqlDataAdapter da ;
   private DataSet ds = new DataSet();

   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.panel1 = new System.Windows.Forms.Panel();
    this.button4 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button1 = new System.Windows.Forms.Button();
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.panel2 = new System.Windows.Forms.Panel();
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.linkLabel1 = new System.Windows.Forms.LinkLabel();
    this.label1 = new System.Windows.Forms.Label();
    this.panel1.SuspendLayout();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.panel2.SuspendLayout();
    this.SuspendLayout();
    //
    // panel1
    //
    this.panel1.Controls.Add(this.button4);
    this.panel1.Controls.Add(this.button3);
    this.panel1.Controls.Add(this.button2);
    this.panel1.Controls.Add(this.button1);
    this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
    this.panel1.Location = new System.Drawing.Point(0, 0);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(392, 40);
    this.panel1.TabIndex = 0;
    //
    // button4
    //
    this.button4.Location = new System.Drawing.Point(224, 8);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(48, 24);
    this.button4.TabIndex = 3;
    this.button4.Text = "过滤";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    //
    // button3
    //
    this.button3.Location = new System.Drawing.Point(152, 8);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(48, 24);
    this.button3.TabIndex = 2;
    this.button3.Text = "排序";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(88, 8);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(48, 24);
    this.button2.TabIndex = 1;
    this.button2.Text = "更新";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(8, 8);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(56, 24);
    this.button1.TabIndex = 0;
    this.button1.Text = "查找";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // dataGrid1
    //
    this.dataGrid1.BackgroundColor = System.Drawing.Color.DarkGray;
    this.dataGrid1.CaptionBackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
    this.dataGrid1.CaptionForeColor = System.Drawing.Color.Navy;
    this.dataGrid1.CaptionText = "中国.武汉动力科技有限公司   http://www.whpower.cn";
    this.dataGrid1.DataMember = "";
    this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.dataGrid1.GridLineColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(0)));
    this.dataGrid1.HeaderBackColor = System.Drawing.Color.Navy;
    this.dataGrid1.HeaderForeColor = System.Drawing.Color.Red;
    this.dataGrid1.Location = new System.Drawing.Point(0, 40);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(392, 326);
    this.dataGrid1.TabIndex = 1;
    //
    // panel2
    //
    this.panel2.AllowDrop = true;
    this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
     | System.Windows.Forms.AnchorStyles.Right)));
    this.panel2.Controls.Add(this.label1);
    this.panel2.Controls.Add(this.pictureBox1);
    this.panel2.Controls.Add(this.linkLabel1);
    this.panel2.Location = new System.Drawing.Point(0, 246);
    this.panel2.Name = "panel2";
    this.panel2.Size = new System.Drawing.Size(392, 114);
    this.panel2.TabIndex = 2;
    //
    // pictureBox1
    //
    this.pictureBox1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
    this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
    this.pictureBox1.Location = new System.Drawing.Point(104, 34);
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(192, 72);
    this.pictureBox1.TabIndex = 1;
    this.pictureBox1.TabStop = false;
    //
    // linkLabel1
    //
    this.linkLabel1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
    this.linkLabel1.Location = new System.Drawing.Point(104, 8);
    this.linkLabel1.Name = "linkLabel1";
    this.linkLabel1.Size = new System.Drawing.Size(128, 16);
    this.linkLabel1.TabIndex = 0;
    this.linkLabel1.TabStop = true;
    this.linkLabel1.Text = "武汉动力科技有限公司";
    this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
    this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
    //
    // label1
    //
    this.label1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
    this.label1.Location = new System.Drawing.Point(192, 88);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(104, 16);
    this.label1.TabIndex = 2;
    this.label1.Text = "我们一直在努力!";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.AutoScroll = true;
    this.ClientSize = new System.Drawing.Size(392, 366);
    this.Controls.Add(this.panel2);
    this.Controls.Add(this.dataGrid1);
    this.Controls.Add(this.panel1);
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.Name = "Form1";
    this.Text = "ADO.NET断开模式[查找.更新.排序.过滤]武汉动力科技软件研发部";
    this.panel1.ResumeLayout(false);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.panel2.ResumeLayout(false);
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }


   //查找
   private void button1_Click(object sender, System.EventArgs e)
   {
    SqlConnection conn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
    SqlCommand cmd = new SqlCommand("select * from Employees",conn);
    da = new SqlDataAdapter(cmd);
    da.Fill(ds,"Employees");
    this.dataGrid1.DataSource = ds.Tables[0];

   }

   //更新

   private void button2_Click(object sender, System.EventArgs e)
   {
    SqlCommandBuilder bu = new SqlCommandBuilder(da);
    if(ds.GetChanges()!= null)
    {
     da.Update(ds.GetChanges(),"Employees");
     ds.AcceptChanges();

    }
   }

   //排序
   private void button3_Click(object sender, System.EventArgs e)
   {
    DataView dv = ds.Tables[0].DefaultView;
    dv.Sort = "Title";
    this.dataGrid1.DataSource = dv;
   }

   //过滤
   private void button4_Click(object sender, System.EventArgs e)
   {
    DataView dv = ds.Tables[0].DefaultView;
    dv.RowFilter = "Firstname like '郭%'";
    this.dataGrid1.DataSource = dv;
   }

   private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
   {
    //创建了一个文字链接,原链接颜色为红色,点击后颜色为蓝色。
    try
    {
     VisitLink();
    }
    catch (Exception sus )
    {
     //        MessageBox.Show("Unable to open link that was clicked.");
    }
   }

   private void VisitLink()
   {
    linkLabel1.LinkVisited = true;
    System.Diagnostics.Process.Start("http://www.whpower.cn");
   }
}
}