Dataset.GetChanges(DataRowState.Modified) return null
我想问一下C.NET的情况
为什么我的dataset.getchanges(datarowstate.modified)返回空值?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | </p> <p> 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; </p> <p> namespace test_for_error { public partial class Form1 : Form { private SqlDataAdapter _DataAdapter; private DataSet _DataSet; </p> <wyn> public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(@"Data Source=BEB7WILLOW\BEB7WILLOW; Database=Cost_Estimate_DB; Trusted_Connection=No; User ID=sa; password=s@password1"); _DataAdapter = new SqlDataAdapter("select client_name from cost_estimate", cn); _DataSet = new DataSet(); _DataAdapter.Fill(_DataSet,"cost_estimate"); this.txtClient.DataBindings.Add("Text", _DataSet,"cost_estimate.client_name", true, DataSourceUpdateMode.OnPropertyChanged); } private void btnSave_Click(object sender, EventArgs e) { _DataSet.AcceptChanges(); DataSet DatasetChanges = _DataSet.GetChanges(DataRowState.Modified); } } |
}
在DataSet.getChanges之前调用DataSet.AcceptChanges,这将重置行状态,因此不会修改任何内容。
"The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed."
试一试
1 2 3 4 5 | private void btnSave_Click(object sender, EventArgs e) { DataSet DatasetChanges = _DataSet.GetChanges(); _DataSet.AcceptChanges(); } |
@肖恩
我照你说的做了,而且效果很好。但我还有一个问题,每次我用这个代码提取子集时
1 2 3 4 | if (_Ds.HasChanges()) { DataSet DsChanges = _Ds.GetChanges(DataRowState.Modified); } |
当我检查新数据集中的数据时,dschanges提取的数据仍然是加载表单时的原始数据吗?这应该是我在美国做的改变。最后我使用了存储过程,这不是解决我问题的优雅方式。