删除功能 1208

137 阅读1分钟
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication58_复习dgv
{
    public partial class Form1 : Form
    {
        SqlDataAdapter adapter;
        DataSet ds;
        SqlConnection conn;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 加载方法调用
            loading();
        }

        // 加载方法定义
        public void loading() { 

            // 数据集
            ds = new DataSet();

            // sql语句
            String sql = "select * from sanguo";

            // 连接对象
            // 连接类 连接对象名称 = new 连接类(连接字符串)
            String connString = "Data Source=.;Initial Catalog=dbok;Persist Security Info=True;User ID=sa;Password=root123123";
            conn = new SqlConnection(connString);


            // 小车
            // 小车类 小车名称 = new 小车类(sql,连接对象); 
            adapter = new SqlDataAdapter(sql,conn);

            // 小车卸货到仓库中
            adapter.Fill(ds,"hero");



        
            // 核心操做
            // 关闭自动列的添加
            dataGridView1.AutoGenerateColumns = false;

            // 控件绑定数据源
            // 控件对象.DataSource = 仓库.Tables[表名称]
            dataGridView1.DataSource = ds.Tables["hero"];



        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tsmiSubmit_Click(object sender, EventArgs e)
        {
            // 更新数据
            // 实例化一个更新用的对象
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

            // 小车更新数据
            adapter.Update(ds, "hero");

            // 提示内容
            MessageBox.Show("更新成功");
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            // 最终目的,获得选中行的第一个单元格里面的值
            // 控件.当前行

            // 1,获得当前行
            DataGridViewRow currentRow = dataGridView1.CurrentRow;

            // 2,获该行的第0号索引的单元格
            DataGridViewCell currentCell = currentRow.Cells[0];

            // 3,获得该单元格的Value值
            String value = currentCell.Value.ToString();

            // 4,通过连接式删除数据
            // 通过连接对象删除这个数据
            String sql = "delete from sanguo where id = " + value;

            // 新建一个执法者
            SqlCommand cmd = new SqlCommand(sql,conn);

            // 打开连接
            try
            {
                // 可能会出错的问题
                conn.Open();

                // 执法者方法调用
                int n = cmd.ExecuteNonQuery();

                // 输出一下结果
                MessageBox.Show("删除成功,影响行数为:" + n);

            }
            catch 
            {
                // 提示出错
                MessageBox.Show("error");

            }
            finally { 
                // 关闭连接对象
                conn.Close();
            }


 
   


        }
    }
}