利用C#直接在datagridview控件中修改数据,问题是,提示修改成功,可是数据库里却是没修改到

weixin_39056641 2017-06-05 10:11:22
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LYSQXJcw
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
SqlConnection conn;
SqlDataAdapter adapter;
int intindex = 0;

private void Form7_Load(object sender, EventArgs e)
{

}

private void button1_Click_1(object sender, EventArgs e)
{
conn = new SqlConnection("server=10.141.省略");
SqlDataAdapter sda = new SqlDataAdapter("Select id AS 序号,name as 姓名,ZW AS 岗位工资,JB AS 薪级工资,GT AS 国统津贴,GZ AS 岗位津贴,SH AS 生活补贴,YBYJ AS 夜班援疆补助奖励补资,ZJmoney1 AS 月工资合计1,FZ AS 房租,YB AS 医保,SBXY AS 暂扣社保及职业年金,GJJ AS 公积金,SHF AS 伙食费,KOUHEJI AS 扣款合计,ZJmoney2 AS 月工资合计2,SHIFAHEJI1 AS 实发1,YJLJX AS 月奖励性绩效,TX AS 通讯费,JBW AS 加班误餐,LB AS 领班补贴,CB AS 测报检查员,RY AS 人影应急值班费,FJ AS 法定节假日值班费,ZJmoney2 AS 月工资合计2,SHIFAHEJI2 AS 实发2 from SYmoney ", conn);



DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.RowHeadersVisible = false;
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
dataGridView1.Columns[i].Width = 84;
}
button1.Enabled = false;
dataGridView1.Columns[0].ReadOnly = true;
}
private DataTable dbconn(string strSql)
{
if (conn.State == ConnectionState.Open)
conn.Close();
this.adapter = new SqlDataAdapter(strSql, conn);
DataTable dtSelect = new DataTable();
int rnt = this.adapter.Fill(dtSelect);
return dtSelect;
}

private void button2_Click_1(object sender, EventArgs e)
{
if (dbUpdate())
{
MessageBox.Show("修改成功!");
}
}
private Boolean dbUpdate()
{
string strSql = "select * from SYmoney";
DataTable dtUpdate = new DataTable();
dtUpdate = this.dbconn(strSql);
DataTable dtShow = new DataTable();
dtShow = (DataTable)this.dataGridView1.DataSource;
dtUpdate.ImportRow(dtShow.Rows[intindex]);
SqlCommandBuilder CommandBuiler;
CommandBuiler = new SqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
dtUpdate.AcceptChanges();
return true;
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int intindex = e.RowIndex;


}

private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
...全文
439 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39056641 2017-06-06
  • 打赏
  • 举报
回复
用到DataTable的ImportRow方法和DataAdapter对象的Update方法。实现的过程是通过DataTable的ImportRow方法将更改后的数据复制到一个DataTable中,然后通过DataAdapter对象的Update方法,将DataTable中的数据更新到数据库中。
正怒月神 2017-06-06
  • 打赏
  • 举报
回复
string strSql = "select * from SYmoney"; DataTable dtUpdate = new DataTable(); dtUpdate = this.dbconn(strSql); strSql只是一个查询语句
weixin_39056641 2017-06-06
  • 打赏
  • 举报
回复
用SqlCommandBuilder自动为SqlDataAdapter生成Insert、Update、Delete命令
wang_peng_yl 2017-06-06
  • 打赏
  • 举报
回复
你问错问题了吧,你的sql语句分明是查询
  • 打赏
  • 举报
回复
没有看到你用Inset 语句向数据库插入数据啊。
自信男孩 2017-06-05
  • 打赏
  • 举报
回复
建议转到C#讨论区

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧