以 24-073110-XX 格式将数据插入表时出现问题???
我需要帮助将 id 插入 ASP.net mvc (C#) 中的数据库。这里的id是主键,它的格式应该是24-073110-XX
,其中XX代表一个数值,应该增加1。
我应该如何插入这种格式的id?
I need help insering an id into the database in ASP.net mvc (C#). Here theid is the primary key and it should be in the format 24-073110-XX
, where XX represents a numeric value which should be incremented by 1.
How should I insert the id in this format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Rob 所说 - 不要将整个大标识符存储在表中 - 只需存储更改的部分 - 连续数字。
如果您确实需要在表中显示整个标识符,例如为了显示它,您可以使用计算列:
这样,您的 INT IDENTITY 将用作 INT 并始终包含数值,并且它将由 SQL 自动递增服务器。
然后,您的
DisplayID
字段将包含如下值:由于它是持久字段,因此它现在是表的一部分,您可以对其进行查询,甚至可以在其上放置索引以加快查询速度:
更新:
我绝对不会使用
DisplayID
作为主键 - 这就是ID IDENTITY
列的优点对于,
在
DisplayID
上创建索引与在表中的任何其他列上创建索引没有什么不同,实际上:As Rob said - don't store the whole big identifier in your table - just store the part that changes - the consecutive number.
If you really need that whole identifier in your table, e.g. for displaying it, you could use a computed column:
This way, your INT IDENTITY will be used as an INT and always contains the numerical value, and it will be automatically incremented by SQL Server.
Your
DisplayID
field will then contain values like:Since it's a persisted field, it's now part of your table, and you can query on it, and even put an index on it to make queries faster:
Update:
I would definitely not use
DisplayID
as your primary key - that's what theID IDENTITY
column is great forto create an index on
DisplayID
is no different than creating an index on any other column in your table, really:如果数据的
24-073110-
部分始终相同,则将其存储在数据库中几乎没有意义。鉴于您已经说过XX
组件是一个递增 1 的数值,我建议您的表创建方式与此类似:这样,您可以让数据库担心插入唯一的自动增加主键的值。
If the
24-073110-
part of the data is always going to be the same, there's little to no point in storing it in the database. Given that you've said that theXX
component is a numeric value that increments by one, I'd suggest having your table created similarly to this:This way, you can let the database worry about inserting unique automatically incrementing values for your primary key.