数据集自动增量问题 [英] Dataset autoincrement issue

查看:94
本文介绍了数据集自动增量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server数据库有几个表。其中一个有ID(主键),名称和其他列,为了简单,我不会在这里提到。 ID列是自动递增,唯一的,当我使用SQL Server管理工作室添加一些行时,ID列正确递增。数据库是旧的,当前的自动增量是在1244左右。

I have a SQL Server Database with several tables. One of them has "ID" (primary key), "Name" and other columns that i won't mention here for sake of simplicity. "ID" column is auto increment, unique and when i add some row using "SQL Server management studio", "ID" column increments properly. Database is old and current auto increment is at 1244 or so.

现在,我创建了一个C#项目,使用TYPED数据集处理数据库中的数据。我的数据库开始为空,数据集使用表适配器填充,使用我的程序添加新行,但有一个我从来没有绊到到目前为止的问题:当我的程序添加新行到Dataset,然后更新数据库(使用表适配器) ID列在数据库中获得正确的自动递增数字(1245,1246等),但我的数据集中的ID列获取-1,-2改为!有什么问题?我如何告诉我的数据集使用数据库指定的自动增量种子,而不是生成自己的NEGATIVE(?)主键号码?

Now, i have created a C# project that uses TYPED Dataset to work with data from database. My database starts empty, dataset is filled using table adapters, new rows are added using my program but there's a problem i have never stumbled upon so far: when my program adds new row to Dataset, then updates database (using table adapter), "ID" column in my database gets correct auto-incremented number (1245,1246 etc), BUT my "ID" column in dataset gets "-1", "-2" instead! What's the problem? How can i tell my dataset to use auto-increment seed specified by database instead generating it's own NEGATIVE (???) primary key numbers?

/ strong>
我使用这个来获取和比较行:

I get and compare rows using this:

dsNames.tbNamesRow[] TMP = basedataset.tbNames.Select() as dsNames.tbNamesRow[];
foreach (dsNames.tbNamesRow row in TMP)
{
    string Name = row.Name;
    bool Found = Name == Search;
    if (CompareDelegate != null)
        Found = CompareDelegate(Name, Search);
    if (Found)
    {
        int ID = row.ID;
        break;
    }
}


推荐答案

原始评论是不正确的,我假设您从数据库检索值,并且THAT数据集中有不正确的值。

My original comment was kind of incorrect, I assumed you were retrieving the value from the database and THAT dataset had incorrect values in it.

ADO.NET处理防止冲突的方式与它断开的数据集,它分配负的IDENTITY列值,因为它不知道一个可能的正数,它不是冲突,因为它断开连接。

The way ADO.NET deals with preventing collisions with it's disconnected dataset, it assigns negative IDENTITY column values, because it wouldn't know a possible positive number that IS NOT a collision as it's disconnected. These (negative) values are unique in terms of that transaction.

当您尝试并提交更改时,ADO.NET引擎会确定正确的SQL以产生正确的结果。

When you try and commit your changes, the ADO.NET engine determines the proper SQL to produce the correct result.

这篇关于数据集自动增量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆