利用C#直接在datagridview控件中修改数据,问题是,提示修改成功,可是数据库里却是没修改到
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();
}
}
}