• C#2005中使用控件DataGridView实现对数据库增删改查操作


    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() //此方法作用于删除
    {
         SqlParameter para
    = new SqlParameter();
         da.DeleteCommand
    = conn.CreateCommand();
         da.DeleteCommand.CommandText
    = "delete from region 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(
    "修改失败!");
         }
    }
    }
    }

    作者:zeke     
              出处:http://zhf.cnblogs.com/
              本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

  • 相关阅读:
    各个地区的经纬度
    frp代理windows远程桌面
    ios软件上架备忘
    电力知识收集
    卸载阿里云云盾(安骑士)(Agent)
    chrome 调试动画方式
    gin nginx 获取不到真实ip 一直是127.0.0.1 c.ClientIP()获取不到真实ip
    https双向认证 本地创建自定义证书 nginx配置https双向认证
    win10开机自动运行exe程序
    Gin框架之Session的使用(详解)
  • 原文地址:https://www.cnblogs.com/ZHF/p/zhangfeng.html
Copyright © 2020-2023  润新知