我如何通过Asp.Net在Sql中更新Id [英] How I Update Id In Sql By Asp.Net

查看:65
本文介绍了我如何通过Asp.Net在Sql中更新Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


I create a table in asp.net  I want to store Id into database by default  id is a primary key Column in database and I also create  order  by procedure  what I do on every insertion Id automatically changed For Eg I insert data  this time id is 1 and  2nd time id 2   the user cant see her id . and I dont want to use AutoIncrement In Sql

推荐答案

如果在SQL中不使用IDENTITY(自动增量被称为SSMS中的身份字段)然后你必须克服许多可能的问题。

首先,在分配之前你永远不应该告诉用户一个ID - 因为SQL的主要优点是它可以在多用户环境中工作,如果你同时创建了两个用户,它们不能同时拥有下一个可用的数字:一个将获得n而另一个必须有n + 1。这意味着你无法在实际创建记录之前告诉用户一个身份证号码!

是的,你可以创建一个临时记录,它将分配一个ID,并告诉他们 - 那么一个人将获得n而下一个n + 1并且一切正常 - 除非第一个决定不继续进行并拔出或断开连接 - 你最终会出现数字上的空白,我假设你也试图避免。



第二个是你需要在某处存储下一个免费号码,并提供一个分配它并移动的存储过程它打开,以便两个用户不能同时执行它。这可能变得非常复杂,特别是如果你的系统需要可扩展 - 在多个SQL服务器上传播它会变得非常讨厌!



单向循环不使用整数 - 使用GUID并从ASP.NET代码中分配 - 但是用户不喜欢它们作为用户ID,因为它们很长而且很难记住。



最后,这将取决于你为什么要告诉他们用户一个ID值 - 大多数根本不需要知道 - 只需使用电子邮件或用户名登录,一切都应该没问题,然后从那时起,在代码中使用id值。
If you don't use IDENTITY in SQL (an autoincrement is called and "identity field" in SSMS) then there are a lot of possible problems you have to overcome.
The first is that you should never tell a user an ID before it is assigned - because the major advantage of SQL is that it works in a multiuser environment, and if you have two users being created at the same time, they can't both have "the next available number": one will get "n" and the other must have "n+1". Which means that you can't tell a user an Id number in advance of actually creating the record!
Yes, you could create a "temporary" record which will assign an ID, and tell them that - so one will then get "n" and the next "n+1" and it all works fine - except if the first one decides not to go ahead and pulls out, or disconnects - you end up with gaps in your numbers, which I assume you are also trying to avoid.

The second is that you need to store the "next free number" somewhere, and provide a stored procedure that allocates it and moves it on so that two users can't execute it at the same time. This can get quite complex, particularly if your system needs to be scalable - spreading this across multiple SQL servers can get quite nasty!

One way round is not to use integers - use a GUID instead and allocate that from your ASP.NET code - but users don't like them as a userID because they are long and complicated to remember.

In the end, this is going to depend on why you want to tell they user an ID value at all - most don't need to know at all - just use the email or a username for log in and all should be fine, then use the id value in your code from then on.


在插入表中最后一行的id之前,将其递增1,然后用新记录存储它。

您可以使用Store Procedures来解决您的问题。
Simply before inserting fetch the id of last row in the table and increment it by 1 and then store it with new record.
You Can use Store Procedures for this to solve your problem.


检查以下链接

http://angelo laris.blogspot.ae/2013/05/sql-autoincrement-without-using.html [ ^ ]



http://stackoverflow.com/questions/5069548/how-can-i-auto-increment-a-column -without-using-identity [ ^ ]



http://stackoverflow.com/questions/6084572/sql-server-how-to-set-自动增量 - 创建后表 - 没有数据丢失 [ ^ ]
check below link
http://angelolaris.blogspot.ae/2013/05/sql-autoincrement-without-using.html[^]

http://stackoverflow.com/questions/5069548/how-can-i-auto-increment-a-column-without-using-identity[^]

http://stackoverflow.com/questions/6084572/sql-server-how-to-set-auto-increment-after-creating-a-table-without-data-loss[^]


这篇关于我如何通过Asp.Net在Sql中更新Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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