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 + "\r\n";
textBox4.Text += "----------------------------------------------------------------------------------------" + "\r\n";
}
else if (item.RowState == DataRowState.Modified)
{
string str = @"
UPDATE Region
SET
RegionDescription = '" + item.ItemArray[1] + @"'
WHERE RegionID=" + item.ItemArray[0];
textBox4.Text += str + "\r\n";
textBox4.Text += "----------------------------------------------------------------------------------------" + "\r\n";
}
else if (item.RowState == DataRowState.Deleted)
{
string str = @"
DELETE FROM Region
WHERE RegionID=" + item[0, DataRowVersion.Original];
textBox4.Text += str + "\r\n";
textBox4.Text += "----------------------------------------------------------------------------------------" + "\r\n";
}
}
//这里增加事务执行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

最新文章

  1. 有关binlog的那点事(二)(mysql5.7.13)
  2. .NET 缩略图服务器 ResizingServer
  3. meeting room I & II
  4. 图像分割之(三)从Graph Cut到Grab Cut
  5. HTML5之缓存
  6. java 复用类的三种方式区别 组合,继承,代理的区别
  7. C#设计模式-创建型模式(转)
  8. Dictionary到List转换中的性能问题 转
  9. iOS 之 定时器
  10. 11g oracle 用户密码过期问题
  11. [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)
  12. js中addEventListener第三个参数涉及到的事件捕获与冒泡
  13. c#获取数组中最大的元素
  14. Erlang调度器细节探析
  15. Maven-01: Maven入门
  16. nginx proxy_pass 代理域名
  17. [JSON]初识JSON
  18. Java的家庭记账本程序(E)
  19. Android Bluetooth hci 命令分析
  20. C# 让String.Contains忽略大小写

热门文章

  1. SQL遍历日期
  2. noip模拟测试21
  3. js原始数据类型有哪些,引用数据类型有哪些
  4. Java面向对象15——内部类
  5. [SQL]基本表的定义及其完整性约束
  6. Python之简单的神经网络
  7. shodan搜索
  8. python3.9 manage.py runserver 报错问题解决
  9. 解密优酷智能生产技术,看 AI 赋能内容数字化
  10. SQL 练习9