gpt4 book ai didi

c# - 删除其中一条后对剩余记录重新编号

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:52:07 24 4
gpt4 key购买 nike

我在使用 Entity Framework ASP.NET MVC 的 SQL Server 中有一个数据库。我有一个名为“S No”的列。代表序号。现在,当我从数据库中删除一条记录时,显然是“S 号”。跳过记录序列号。我想重新编号“S号”。使用我的 Controller 的列。有什么算法可以让我轻松地重新编号吗?

这是 AddFeature() 方法的代码,我在其中对“S 号”进行编号。专栏。

  public ActionResult AddFeature(Models.SoftwareFeatureList s)
{
ViewBag.Username = uname;
ViewBag.UserType = utype;
try
{
c = new DatabaseZTBLEntities();
s.FormId = formid;
int identity = c.SoftwareFeatureLists.Where(u=>u.FormId.Equals(formid)).Count() +1;
s.S_No_ = identity;
c.SoftwareFeatureLists.Add(s);

c.SaveChanges();
}
catch(Exception e)
{
ViewBag.Error = e.Message;
return View("error");
}
finally
{
c.Database.Connection.Close();
}
return Redirect("FeaturesListPage");
}

最佳答案

两种解决方案:

  1. 不要将 S_no 作为数据库中的一列,而是在读取操作返回记录时动态生成它的值。
  2. 如果您仍想将 S_no 保留在数据库中,则需要在任何删除发生时直接更新您的数据库(使用触发器)。

    row_number() over(partition by formId order by formId)

关于c# - 删除其中一条后对剩余记录重新编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47387567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com