This may or may not be a noob question, so I'm sure it should be solved easily enough.
这可能是也可能不是noob问题,所以我确信它应该很容易解决。
I'm trying to use a table in a .mdf database for a contacts list programme I'm writing. For the ID for each of the entries I put, I've got an Identity seed of 1000, with in increment of 1. This works fine when I input things directly into the table. The first entry is 1000, the second is 1001 and so on. I put some text boxes onto the form, and dragged each data source from the table onto their respective text box, so that new entries can be added from the form. The first two entries, which I added directly into the table, show up fine on the form, with their ID as I've previously stated. It's when I try to add a new entry using the form and the controls that I've created that problems arise.
我正在尝试使用.mdf数据库中的表来创建我正在编写的联系人列表程序。对于我放入的每个条目的ID,我有一个1000的身份种子,增量为1.当我直接在表中输入内容时,这种方法很有效。第一个条目是1000,第二个条目是1001,依此类推。我在表单上放了一些文本框,并将表中的每个数据源拖到各自的文本框中,以便可以从表单中添加新条目。我直接添加到表格中的前两个条目在表单上显示正常,其ID与我之前说过的一样。当我尝试使用表单和我创建的控件添加新条目时出现问题。
When I add a new entry, the Contact ID for the new entry is set to -1, then -2 and so on. Why is this happening, and how can I fix it?
添加新条目时,新条目的联系人ID设置为-1,然后设置为-2,依此类推。为什么会发生这种情况,我该如何解决?
Relevant snips:
相关剪辑:
First entry is in there and works fine
第一次进入并且工作正常
New entry has an ID of -1 for some reason
由于某种原因,新条目的ID为-1
2 个解决方案
#1
1
It is default value for IDENTITY fields.
它是IDENTITY字段的默认值。
As far as you cannot know which will be the value of an IDENTITY fields unless you insert the record, by default it show -1
除非您插入记录,否则您无法知道哪个是IDENTITY字段的值,默认情况下它显示-1
The best way to avoid it, it now showing this field when you dateset is on INSERT mode.
避免它的最佳方法是,当dateset处于INSERT模式时,它现在显示此字段。
In fact, if you try to set it value manually, you will get an error.
实际上,如果您尝试手动设置它,则会出现错误。
#2
0
Make ID field read-only or don't show it altogether. The INSERT sql for your form should not include identity, i.e.
使ID字段为只读或不完全显示。您表单的INSERT sql不应包含标识,即
Insert into contacts (name, company, telephone, mobile, email)
Output INSERTED.*
Values (. . . . .)
If you in c# do command.ExecuteReader
using above sql, you will get back all values including ID
如果你在c#中使用上面的sql执行command.ExecuteReader,你将获得包括ID在内的所有值
#1
1
It is default value for IDENTITY fields.
它是IDENTITY字段的默认值。
As far as you cannot know which will be the value of an IDENTITY fields unless you insert the record, by default it show -1
除非您插入记录,否则您无法知道哪个是IDENTITY字段的值,默认情况下它显示-1
The best way to avoid it, it now showing this field when you dateset is on INSERT mode.
避免它的最佳方法是,当dateset处于INSERT模式时,它现在显示此字段。
In fact, if you try to set it value manually, you will get an error.
实际上,如果您尝试手动设置它,则会出现错误。
#2
0
Make ID field read-only or don't show it altogether. The INSERT sql for your form should not include identity, i.e.
使ID字段为只读或不完全显示。您表单的INSERT sql不应包含标识,即
Insert into contacts (name, company, telephone, mobile, email)
Output INSERTED.*
Values (. . . . .)
If you in c# do command.ExecuteReader
using above sql, you will get back all values including ID
如果你在c#中使用上面的sql执行command.ExecuteReader,你将获得包括ID在内的所有值