在 C# 中从绑定到数据库的列表框保存数据
我有一个使用 SQL Server CE 绑定到 Windows CE 应用程序中的列表框的数据集。
数据集中只有一张名为“Codigos”的表,其中包含两个字段:id 和“code”字段。
我手动将一些数据插入表中,然后将其很好地加载到列表框中。
当我尝试向列表框中添加新值然后保存它时,就会出现问题。
这是我尝试过的代码:
DatalogicDataSet.CodigosRow newCodigosRow = datalogicDataSet.Codigos.NewCodigosRow();
newCodigosRow.codigo = "003";
datalogicDataSet.Codigos.Rows.Add(newCodigosRow);
这会将带有“003”值的新行加载到列表框中。
我将按钮中的单击事件与更新数据库的代码相关联:
private void button1_Click(object sender, EventArgs e)
{
datalogicDataSet.AcceptChanges();
}
我也尝试了这个:
private void button1_Click(object sender, EventArgs e)
{
try
{
this.codigosBindingSource.EndEdit();
this.codigosTableAdapter.Update(this.datalogicDataSet.Codigos);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}
}
但是它们都不起作用,数据库没有更新。
有什么建议吗?
I have a dataset bounded to a listbox in a Windows CE application using SQL Server CE.
There is only one table in the dataset named "Codigos" with two fields: an id and a "code" field.
I inserted manually some data into my table and loads fine into the listbox.
The problem happens when I try to add a new value to the listbox and then save it.
This is the code I've tried:
DatalogicDataSet.CodigosRow newCodigosRow = datalogicDataSet.Codigos.NewCodigosRow();
newCodigosRow.codigo = "003";
datalogicDataSet.Codigos.Rows.Add(newCodigosRow);
This loads a new row into the listbox with the "003" value.
I associated to a click event in a button the code to update the database:
private void button1_Click(object sender, EventArgs e)
{
datalogicDataSet.AcceptChanges();
}
I also tried this:
private void button1_Click(object sender, EventArgs e)
{
try
{
this.codigosBindingSource.EndEdit();
this.codigosTableAdapter.Update(this.datalogicDataSet.Codigos);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}
}
But neither of them worked, the database is not updated.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
Try:
万一有人碰巧发现同样问题的解决方案。
问题是数据库正在正确更新,但不是我计算机中的数据库,而是 Windows CE 终端中的数据库。发生这种情况是因为应用程序在通过 Visual Studio 2008 执行时将数据库复制到 Windows CE 终端设备中。
因此,每次我执行该应用程序时,它都会使用原始数据而不是更新的数据加载计算机中的数据库,所以看起来工作错误,但终端设备中的更新正确。
当您生成生产程序并将其安装到使用 Windows CE 的设备时,它不会出现该问题,并且会正确更新数据库。
The solution in case anyone happens to find the same problem.
The problem was that the database was being updated correctly, but not the one in my computer, only the one in the terminal with windows CE. This happens because the application copies the database into the Windows CE terminal device when it's executed through Visual Studio 2008.
So each time I executed the application it loaded the database in my computer with the original data instead of the updated data, so it seemed to work wrong, but the one in the terminal device was being correctly updated.
When you generate your production program and install it into the device with Windows CE it won't have that problem and will update the database correctly.