简体   繁体   English

C#WinForms-DataGridView / SQL Compact-主键列中的负整数

[英]C# WinForms - DataGridView/SQL Compact - Negative integer in primary key column

I'm just getting dirty in WinForms, and I've discovered, through a lovely tutorial, the magic of dragging a database table onto the design view of my main form. 我只是在WinForms中变得肮脏,并且通过一个可爱的教程发现了将数据库表拖到主窗体的设计视图中的魔力。 So, all is lovely, I've got my DataGridView with all of the columns represented beautifully. 因此,一切都很可爱,我的DataGridView的所有列都得到了精美的展示。

BUT... 但...

When I run my application against this brand new, empty .sdf (empty save for the two tables I've created, which are themselves empty), I get a -1 in the column corresponding to my primary key/identity column whenever I try to create that first record. 当我针对这个全新的空.sdf(我创建的两个表的空保存,它们本身都是空的)运行应用程序时,每当我尝试时,在与主键/标识列相对应的列中都会得到-1创建第一条记录。

Any idea why this might be happening? 知道为什么会这样吗? If it helps, the column is an int . 如果有帮助,则该int

@Brian -1 is a good choice for the default value since no "real" rows are likely to have identities less than zero. @Brian -1是默认值的不错选择,因为没有“真实”行的标识可能小于零。 If it defaulted to 0 or 1 then there'd be a chance that it'd clash with an existing row, causing a primary key violation. 如果默认值为0或1,则有可能与现有行发生冲突,从而导致主键冲突。

For applications that stay offline and create multiple rows before saving, a common practice is to continue counting backwards (-2, -3, -4) for each new row's identity. 对于保持脱机状态并在保存之前创建多行的应用程序,通常的做法是继续对每个新行的标识进行反向计数(-2,-3,-4)。 Then when they're saved, the server can replace them with the true "next" value from the table. 然后,在保存它们之后,服务器可以将它们替换为表中真实的“ next”值。

Since it is an Identity column and you haven't saved it to the database yet it is -1. 由于它是“身份”列,并且您尚未将其保存到数据库中,因此它是-1。 I am assuming here that this is before you save the table back to the database, correct? 我在这里假设这是在您将表保存回数据库之前,对吗? You need to perform the insert before that value will be set correctly. 您需要先执行插入操作,然后才能正确设置该值。

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

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