• DataTable 增加、修改、删除


    using System;
    using System.Data;
    using System.Windows.Forms;
    using DotNet.Utilities;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            DataTableCollection dt = null;
            //查询原始内容
            private void button1_Click(object sender, EventArgs e)
            {
                string sql = @"SELECT r.RegionID, RTRIM(r.RegionDescription)RegionDescription FROM Region AS r";
                dt = SqlHelper.GetTableText(sql, null);
                dataGridView1.DataSource = dt[0];
            }
    
            //增加内容
            private void button2_Click(object sender, EventArgs e)
            {
                DataRow dr = dt[0].NewRow();
                dr["RegionID"] = textBox2.Text;
                dr["RegionDescription"] = textBox3.Text;
                dt[0].Rows.Add(dr);
            }
    
            //修改内容
            private void button3_Click(object sender, EventArgs e)
            {
                DataRow dr = dt[0].Rows[dataGridView1.CurrentRow.Index];
                dr.BeginEdit();
                dr["RegionID"] = textBox2.Text;
                dr["RegionDescription"] = textBox3.Text;
                dr.EndEdit();
    
            }
    
            //删除内容
            private void button4_Click(object sender, EventArgs e)
            {
                dt[0].Rows[dataGridView1.CurrentRow.Index].Delete();
            }
    
            //保存内容,根据DataTable生成Sql语句
            private void button5_Click(object sender, EventArgs e)
            {
                textBox4.Clear();
                DataTable dtSave = dt[0].GetChanges();
                if (dtSave != null)
                {
                    foreach (DataRow item in dtSave.Rows)
                    {
                        if (item.RowState == DataRowState.Added)
                        {
                            string str = @"
                                    INSERT INTO Region
                                    (
                                        RegionID,
                                        RegionDescription
                                    )
                                    VALUES
                                    (
                                        " + item.ItemArray[0] + @",
                                        '" + item.ItemArray[1] + @"'
                                    )
                                    ";
                            textBox4.Text += str + "
    ";
                            textBox4.Text += "----------------------------------------------------------------------------------------" + "
    ";
                        }
                        else if (item.RowState == DataRowState.Modified)
                        {
                            string str = @"
                                    UPDATE Region
                                    SET
                                        RegionDescription = '" + item.ItemArray[1] + @"'
                                    WHERE RegionID=" + item.ItemArray[0];
                            textBox4.Text += str + "
    ";
                            textBox4.Text += "----------------------------------------------------------------------------------------" + "
    ";
                        }
                        else if (item.RowState == DataRowState.Deleted)
                        {
                            string str = @"
                                    DELETE FROM Region
                                    WHERE RegionID=" + item[0, DataRowVersion.Original];
                            textBox4.Text += str + "
    ";
                            textBox4.Text += "----------------------------------------------------------------------------------------" + "
    ";
                        }
                    }
                    //这里增加事务执行SQL语句到数据库
                    //dt[0].AcceptChanges();//保存后清除行状态
                }
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                DataRow dr = dt[0].Rows[dataGridView1.CurrentRow.Index];
                textBox2.Text = dr["RegionID"].ToString();
                textBox3.Text = dr["RegionDescription"].ToString();
            }
        }
    }

    只根据DataTable生成Sql语句,Devexpress GridControl控件获取索引用 gridView1.GetSelectedRows()[0]

    数据库操作类使用https://gitee.com/kuiyu/dotnetcodes开源项目中的SqlHelper

  • 相关阅读:
    docker 如何启动空容器
    dotnet5.0 aspnetcore runtime docker镜像
    abp asp.net core 手动获取容器中的服务
    我把代码写在别人的分支了?别慌,有办法解决
    var重复声明为什么后续声明会被忽略
    flex布局不常用到的属性整理
    微信openID和unionID的区别
    js稀疏数组、密集数组、数组空位、手写数组map方法
    vscode格式化代码换行?试试这个配置
    封装一个公共组件并上传到npm仓库
  • 原文地址:https://www.cnblogs.com/liessay/p/12712032.html
Copyright © 2020-2023  润新知