dataGrid更新数据

来源:互联网 发布:淘宝运营总监招聘 编辑:程序博客网 时间:2024/04/29 03:03

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

namespace WindowsApplication1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private DataSet myDs;
  private SqlDataAdapter myDa;

  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.Button updt;
  private System.Windows.Forms.Button conndb;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  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()
  {
   this.updt = new System.Windows.Forms.Button();
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   this.conndb = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
   this.SuspendLayout();
   //
   // updt
   //
   this.updt.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
   this.updt.Location = new System.Drawing.Point(344, 328);
   this.updt.Name = "updt";
   this.updt.Size = new System.Drawing.Size(200, 23);
   this.updt.TabIndex = 0;
   this.updt.Text = "Update to DataBase";
   this.updt.Click += new System.EventHandler(this.updt_Click);
   //
   // dataGrid1
   //
   this.dataGrid1.DataMember = "";
   this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGrid1.Location = new System.Drawing.Point(0, 0);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.Size = new System.Drawing.Size(590, 312);
   this.dataGrid1.TabIndex = 1;
   //
   // conndb
   //
   this.conndb.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
   this.conndb.Location = new System.Drawing.Point(88, 328);
   this.conndb.Name = "conndb";
   this.conndb.Size = new System.Drawing.Size(168, 23);
   this.conndb.TabIndex = 2;
   this.conndb.Text = "Connection DataBase";
   this.conndb.Click += new System.EventHandler(this.conndb_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(592, 366);
   this.Controls.Add(this.conndb);
   this.Controls.Add(this.dataGrid1);
   this.Controls.Add(this.updt);
   this.Name = "Form1";
   this.Text = "Form1";
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

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

  private void updt_Click(object sender, System.EventArgs e)
  {
   try
   {
    SqlCommandBuilder cb = new SqlCommandBuilder();
    cb.DataAdapter = myDa;
    myDa.UpdateCommand = cb.GetUpdateCommand();
    myDa.DeleteCommand = cb.GetDeleteCommand();
    myDa.InsertCommand = cb.GetInsertCommand();
   
    myDa.Update(myDs.Tables["tab"]); 
    MessageBox.Show("Database updated!");
   }
   catch (Exception eupdt)
   {
    MessageBox.Show("没有更新成功,具体原因是:/n"+eupdt.Message);
   }
  }

  private void conndb_Click(object sender, System.EventArgs e)
  {
   SqlConnection conn= new SqlConnection();
   string connstr = "initial catalog = Northwind; data source = 127.0.0.1; uid= sa; pwd=";
   conn.ConnectionString = connstr;
   try
   {

    conn.Open();
    SqlCommand cmd= new SqlCommand();
    cmd.CommandText = "select * from customers";
    cmd.Connection = conn;
    myDa = new SqlDataAdapter();
    myDa.SelectCommand = cmd;
    myDs = new DataSet();

    myDa.Fill(myDs,"tab");
    dataGrid1.DataSource = myDs;
    dataGrid1.DataMember="tab";
    //dataGrid1.DataBind();  //去掉即可,

   }
   catch ( Exception econn)
   {
    MessageBox.Show("数据库连接错误(0)",econn.Message);
   }
   finally
   {
    conn.Close(); 
   }  
  }
 }
}

原创粉丝点击