简体   繁体   English

将列表框中的数据保存到 c# 中的数据库

[英]Saving data from a listbox bounded to a database in c#

I have a dataset bounded to a listbox in a Windows CE application using SQL Server CE.我有一个数据集绑定到使用 SQL 服务器 CE 的 Windows CE 应用程序中的列表框。

There is only one table in the dataset named "Codigos" with two fields: an id and a "code" field.数据集中只有一个名为“Codigos”的表,其中包含两个字段:一个 id 和一个“code”字段。

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.这会将具有“003”值的新行加载到列表框中。

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?有什么建议么?

Try:尝试:

 private void button1_Click(object sender, EventArgs e)
    {
      DatalogicDataSet.CodigosRow newCodigosRow = datalogicDataSet.Codigos.NewCodigosRow();
      newCodigosRow.codigo = "003";
      datalogicDataSet.Codigos.Rows.Add(newCodigosRow);
      datalogicDataSet.AcceptChanges();            
    }

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.问题是数据库正在正确更新,但不是我计算机中的那个,只有终端中带有 windows CE 的那个。 This happens because the application copies the database into the Windows CE terminal device when it's executed through Visual Studio 2008.这是因为应用程序在通过 Visual Studio 2008 执行时将数据库复制到 Windows CE 终端设备中。

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.当您生成生产程序并将其安装到带有 Windows CE 的设备中时,它不会有这个问题,并且会正确更新数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM