C#2005中使用控制項DataGridView實現對資料庫增刪改查操作

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataSource
{
public partial class Form1 : Form
{
     public Form1()
     {
       InitializeComponent();
     }
private DataSet ds = new DataSet();
private SqlConnection conn = null;
private SqlDataAdapter da = null;
private const string DRIVER = "server=.;database=northwind;uid=sa;pwd=sa";
private const string sql_select = "select * from region";
/**//**
* 此方法為將資料庫northwind中的region表的資料查詢出來並放入DataSet中
**/
private void Form1_Load(object sender, EventArgs e)
  {
     conn = new SqlConnection(DRIVER);
     da = new SqlDataAdapter(sql_select,conn);
     da.Fill(ds,"table");
     this.dataGridView1.DataSource = ds.Tables["table"].DefaultView;
}
private bool BtnInsert() //此方法作用於添加
  {
     da.InsertCommand = conn.CreateCommand();
     da.InsertCommand.CommandText = "insert into region(id,ption) values(@id,@ption)";
     da.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
     da.InsertCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
     int count = da.Update(ds);
     bool result = count > 0 ? true : false;
     return result;
}
private void button1_Click(object sender, EventArgs e)
{
     if (this.BtnInsert())//調用此方法
     {
       MessageBox.Show("添加成功!");
     }
     else
     {
       MessageBox.Show("添加失敗!");
    }
}
private bool BtnDelect() //此方法作用於刪除
{
  SqlParameterpara = new SqlParameter();
  da.DeleteCommand = conn.CreateCommand();
  da.DeleteCommand.CommandText = "delete fromregion where regionid=@id";
  para = da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
  para .SourceVersion = DataRowVersion.Original;//擷取原始值
  ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
  int count = da.Update(ds);
  bool result = count > 0 ? true : false;
  return result;
}
private void button2_Click(object sender, EventArgs e)
{
  if (this.BtnDelect())//調用刪除方法
   {
     MessageBox.Show("刪除成功!");
  }
  else
  {
     MessageBox.Show("刪除失敗!");
  }
}
private bool BtnUpdate() //此方法作用於修改
{
  SqlParameter para = new SqlParameter();
  da.UpdateCommand = conn.CreateCommand();
  da.UpdateCommand.CommandText = "update region set regionid=@id,regiondescription=@ption where regionid=@oldid";
  da.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
  da.UpdateCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
  para= da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4, "regionid");
  para .SourceVersion = DataRowVersion.Original;
  int count = da.Update(ds);
  bool result = count > 0 ? true : false;
  return result;
}
private void button3_Click(object sender, EventArgs e)
{
  if (this.BtnUpdate())//調用修改方法
   {
     MessageBox.Show("修改成功!");
  }
  else
  {
     MessageBox.Show("修改失敗!");
  }
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.