更新在带有数据集和表适配器的 SQLCe 3.5 上未生效

发布于 2024-11-29 15:51:04 字数 1234 浏览 0 评论 0原文

我们使用 VS2010 数据连接设计向导来定义与 SQLCe 数据库的连接,其中仅包含一张表 (database="UserMetrics" table="User")。尽我们所能,更新似乎不起作用,我已经浏览过 SO 和 MSDN 上的帖子,但看不到明显的错误...

        //initialize 
        UserMetricsDataSet umDataSet = new UserMetricsDataSet( );
        UserMetricsDataSetTableAdapters.UserTableAdapter umTableAdapter = new    UserMetricsDataSetTableAdapters.UserTableAdapter( );

        // check that test data is there and count is correct
        umTableAdapter.Fill( umDataSet.User );
        UserMetricsDataSet.UserRow umRow = (UserMetricsDataSet.UserRow)umds.User.Rows[0]; 
        int count = umDataSet.User.Rows.Count; //yep its there

        //lets add some rows
        for (int i = 0; i < 100; i++)
            umDataSet.User.AddUserRow( "smith", (float)54, (float)3, 1);

        umds.User.AcceptChanges( );
        //umTableAdapter.Update(umDataSet.User); //tried this also ... no change...

        // there are now 101 rows !!
        int count = umDataSet.User.Rows.Count; //yep its there
        umRow = (UserMetricsDataSet.UserRow)umds.User.Rows[101];

        //lets double check
        umTableAdapter.Fill( umDataSet.User );
        int count = umds.UserMetris.Rows.Count; //huh!!! count==1 ???

We used the VS2010 data connection design wizard to define a connection to a SQLCe database with just one table (database="UserMetrics" table="User"). Try as we might the update doesn't seem to hold, I've been through posts on SO and MSDN but can't see the glaring error...

        //initialize 
        UserMetricsDataSet umDataSet = new UserMetricsDataSet( );
        UserMetricsDataSetTableAdapters.UserTableAdapter umTableAdapter = new    UserMetricsDataSetTableAdapters.UserTableAdapter( );

        // check that test data is there and count is correct
        umTableAdapter.Fill( umDataSet.User );
        UserMetricsDataSet.UserRow umRow = (UserMetricsDataSet.UserRow)umds.User.Rows[0]; 
        int count = umDataSet.User.Rows.Count; //yep its there

        //lets add some rows
        for (int i = 0; i < 100; i++)
            umDataSet.User.AddUserRow( "smith", (float)54, (float)3, 1);

        umds.User.AcceptChanges( );
        //umTableAdapter.Update(umDataSet.User); //tried this also ... no change...

        // there are now 101 rows !!
        int count = umDataSet.User.Rows.Count; //yep its there
        umRow = (UserMetricsDataSet.UserRow)umds.User.Rows[101];

        //lets double check
        umTableAdapter.Fill( umDataSet.User );
        int count = umds.UserMetris.Rows.Count; //huh!!! count==1 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一笑 2024-12-06 15:51:04

我假设 umDataSet 与 umds 相同。

又怎样呢?

UserMetricsDataSet.UserRow umRow = umDataSet.User.NewRow();
umRow["Name"] = "smith";
// etc
umDataSet.User.ImportRow(umRow);
umDataSet.User.AcceptChanges();

或者颠倒顺序怎么办? TableAdapter.Update() 方法根据 DataTable 中的更改将更新发送回数据库。如果您首先接受更改,则每个 DataTable 行上的 RowState 都会重置,因此不会发现任何更新可发送回数据库。

来自 MSDN

为了将修改后的数据发送到数据库,您可以调用 TableAdapter 的 Update 方法。适配器的 Update 方法将更新单个数据表,并根据表中每个数据行的 RowState 执行正确的命令(INSERT、UPDATE 或 DELETE)。

try
{
    umTableAdapter.Update(umDataSet.User);
    umDataSet.User.AcceptChanges();
}
catch (Exception ex)
{
    // TableAdapter.Update() can throw exceptions
}

I'm assuming umDataSet is the same thing as umds.

What about?

UserMetricsDataSet.UserRow umRow = umDataSet.User.NewRow();
umRow["Name"] = "smith";
// etc
umDataSet.User.ImportRow(umRow);
umDataSet.User.AcceptChanges();

Or what about reversing the order? The TableAdapter.Update() method sends updates back to the database based on the changes in the DataTable. If you accept the changes first, then the RowState is reset on each DataTable row, so there are no updates found to send back to the database.

From MSDN:

In order to send the modified data to a database, you call the Update method of a TableAdapter. The adapter's Update method will update a single data table and execute the correct command (INSERT, UPDATE, or DELETE) based on the RowState of each data row in the table.

try
{
    umTableAdapter.Update(umDataSet.User);
    umDataSet.User.AcceptChanges();
}
catch (Exception ex)
{
    // TableAdapter.Update() can throw exceptions
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文