如何为表单分配唯一值 [英] How I can assign unique value to form

查看:105
本文介绍了如何为表单分配唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些申请,当用户点击创建时自动从数据库获取最大值并使用C#递增这是我的源代码

  public   string  GetMaxLPONo()
{
var Maxvalue =( from p in db.tbl_LPO
选择 p.LpoNumber).Max();
if (Maxvalue == null
{
Maxvalue = 2020 ;
}
else
{
Maxvalue = Maxvalue + 1 ;
}

return Maxvalue.ToString();
}



但是当多于一个用户访问系统时,所有用户的RequisitionsID都是相同的,因为仍然每个用户只访问表单并提供重复ID什么是最好的和快捷方法来处理它



我尝试过:



以上代码我试过,但同时多个用户访问相同的申请单将被分配给表格



任何帮助提前感谢

解决方案

从不这样做。

永远不要预先指定一个唯一值:它应该是数据库中的IDENTITY字段,你让数据库本身要小心当你添加一行时。

如果您像预先那样预先分配,那么您将始终遇到多用户访问问题。想想看:在现实生活中实际申请之前,您不需要申请号 - 那么为什么在DB中创建请求之前需要它?


As Griff声明,原因如上所述。



如果他们在填写表格前绝对需要这个号码,那么他们要做的就是创建他们的DB记录。要求提出申请,然后当它完成后,你会用表格的价值更新他们的记录。



在数据库方面,你会有你的表如前所述,为编号设置并使用标识列。这是一个真正简单的数据库模式

  CREATE   TABLE  dbo.Requisitions(
ReqID INT IDENTITY 1 1 NOT NULL
AccountNumber INT NULL
IsComplete BIT NULL
ReqValue1 ...,
ReqValue2 ...,
CONSTRAINT [PK_Requisitions_ID] PRIMARY KEY CLUSTERED ([ReqID] ASC
ON [ PRIMARY ]



请求新请购单您将运行存储过程。然后,当有人使用简单的参数化查询填写表单时,您可以调用此方法,并且在填写表单之前它将具有实际记录编号。

  CREATE   PROCEDURE  dbo.Requisition_Create(
@ AccountNumber INT
@ ReqID INT 输出
AS
BEGIN
INSERT 申请单(AccountNumber,IsComplete)
VALUES @ AccountNumber 0

SELECT @ AccountNumber = SCOPE_IDENTITY ()
END





完成后,你只需运行另一个SP来进行UPDATE,你需要添加到记录中的任何值

 创建  PROCEDURE  dbo.Requisition_Update(
@ ReqID INT
@ ReqValue1 ...,
@ ReqValue2 。 ..
AS
BEGIN
UPDATE 申请
SET ReqValue1 = @ ReqValue1 ,ReqValue2 = @ RaqValue2
WHERE RegID = @ ReqID
END


I have some Requisitions when user click on create automatically get max value from database and increment using C# this is my source code

public string GetMaxLPONo()
       {
           var Maxvalue = (from p in db.tbl_LPO
                           select p.LpoNumber).Max();
           if (Maxvalue == null)
           {
               Maxvalue = 2020;
           }
           else
           {
               Maxvalue = Maxvalue + 1;
           }

           return Maxvalue.ToString();
       }


But when more then one user access the system RequisitionsID will be same for all user because still every user just access form and gave duplication ID what is best and shortcut method to handle it

What I have tried:

Above code i tried but same time when multiple users access same RequisitionsID will be assigned for forms

Any help thanks in advance

解决方案

Never do it like that.
Never pre-assign a unique value: it should be an IDENTITY field in the DB, and you let the database itself take care of it when you add a row.
If you pre-assign as you do, then you will always get problems with multi-user access. Think about it: you don't need the requisition number until you actually make the requisition in real life - so why would you need it before you create it in the DB?


As Griff has stated, for the exact reasons as was stated.

IF they absolutely need that number before completing the form, the thing to do would be to create their DB record when they request to make a requisition, and then when it is completed you would update their record with the values of the form.

On the database side, you would have your table set up and utilize an identity column for the numbering as previously suggested. Here is a real simple db schema

CREATE TABLE dbo.Requisitions (
  ReqID INT IDENTITY(1,1) NOT NULL,
  AccountNumber INT NULL,
  IsComplete BIT NULL,
  ReqValue1 ...,
  ReqValue2 ...,
  CONSTRAINT [PK_Requisitions_ID] PRIMARY KEY CLUSTERED([ReqID] ASC)
) ON [PRIMARY]


And when a new requisition is requested you would run a Stored Procedure. You could then call this when someone fills out the form using a simple parameterized query, and it will have the actual record number before the form is filled out.

CREATE PROCEDURE dbo.Requisition_Create (
  @AccountNumber INT,
  @ReqID INT OUTPUT
) AS
BEGIN
  INSERT Requisitions (AccountNumber, IsComplete)
  VALUES (@AccountNumber, 0)

  SELECT @AccountNumber = SCOPE_IDENTITY()
END



When completed, you would just run another SP to do the UPDATE with whatever values you need to add to the record

CREATE PROCEDURE dbo.Requisition_Update (
  @ReqID INT,
  @ReqValue1 ...,
  @ReqValue2 ...
) AS
BEGIN
  UPDATE Requisitions
  SET ReqValue1 = @ReqValue1, ReqValue2 = @RaqValue2
  WHERE  RegID = @ReqID
END


这篇关于如何为表单分配唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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