ASP.NET 网格视图的问题

发布于 2024-07-13 15:07:33 字数 1402 浏览 5 评论 0原文

我在删除 gridview 时遇到问题。我的表名称为 Doctor Id,姓名,地址,电话.Id是自动生成的字段。添加数据后 当我在 gridview 中显示时,如果从 gridview 中删除任何 id 再说一次,如果我从表单中添加任何新的详细信息,则从 新号码。我的意思是,如果我删除最后一个 id 5,然后再次删除,如果我 添加任何新医生,其 ID 值是 6,而不是 5。我的查询是它 应该从 5 重新开始。这是我的代码。请帮助我。

public class Doctor
{  
  public int Id { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string Phone { get; set; }
}

public static class DoctorDataLayer
{
  public static void AddDoctor(Doctor doctor) 
  {
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // JohannesH: Changed from .ToString() to .ConnectionString
    using(var connection = new SqlConnection(connectionString))
    {
      using (var command = new  SqlCommand("insert into doctor values(@name,@address,@phone)", connection))
      {      
        command.Parameters.AddWithValue("@name", doctor.Name);
        command.Parameters.AddWithValue("@address", doctor.Address);
        command.Parameters.AddWithValue("@phone", doctor.Phone);
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
      }
    }
  }
}
public static class DoctorBusinessLayer
{
  public static void CreateDoctor(string name, string address, string phone) 
  {
    DoctorDataLayer.AddDoctor(new Doctor {Name = name, Address = address, Phone = phone});
  }
}

I have problem with gridview deleting.I have table name Doctor with
Id,Name,Address,Phone.Id is auto generated field.After adding data
when i am displaying in gridview then if delete any id from gridview
Again then if i add any new details from the form its starting from
the new number.I mean if i delete the last id no 5 then again if i
add any new doctor its taking id value 6 not from 5.My query is it
should start again from 5.Here is my code.Pls help me.

public class Doctor
{  
  public int Id { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string Phone { get; set; }
}

public static class DoctorDataLayer
{
  public static void AddDoctor(Doctor doctor) 
  {
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // JohannesH: Changed from .ToString() to .ConnectionString
    using(var connection = new SqlConnection(connectionString))
    {
      using (var command = new  SqlCommand("insert into doctor values(@name,@address,@phone)", connection))
      {      
        command.Parameters.AddWithValue("@name", doctor.Name);
        command.Parameters.AddWithValue("@address", doctor.Address);
        command.Parameters.AddWithValue("@phone", doctor.Phone);
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
      }
    }
  }
}
public static class DoctorBusinessLayer
{
  public static void CreateDoctor(string name, string address, string phone) 
  {
    DoctorDataLayer.AddDoctor(new Doctor {Name = name, Address = address, Phone = phone});
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帝王念 2024-07-20 15:07:33

这是完全正常的数据库行为,与您的 GridView 无关。 如果您对自动生成(标识)列中的间隙有疑问,请使用您自己的逻辑生成唯一 ID,或使用自定义 SQL 脚本检查标识值中的间隙并填补这些间隙。

Transact-SQL 参考中的示例 B 显示了一种方法这。

This is perfectly normal database behaviour and has nothing to do with your GridView. If you have an issue with gaps in autogenerated (identity) columns, either use your own logic to generate unique ID's or use custom SQL scripts to check for gaps in Identity values and fill those gaps.

Example B in the Transact-SQL reference shows a way to do just this.

友欢 2024-07-20 15:07:33

所以Id是由数据库创建的(自动编号)。 当 id 5 使用时,它就用完了。 这是正常行为。

So the Id is created by the database (autonumber). When id 5 is used it's used up. This is normal behavior.

塔塔猫 2024-07-20 15:07:33

正如其他人所指出的,如果这是从数据库自动生成的 ID,那么一旦使用它就不会重新生成,每个 ID 都是唯一的,无论数据是否仍然存在。 如果 ID 被回收,您可能会遇到外部引用的问题,这些引用可能指向具有该 ID 的旧项目,而现在将指向具有重用 ID 的新的不同记录。

通常,您无论如何都不会向用户公开 ID,因此这不是问题。

As other have noted, if this is an autogenerated ID from the DB then once it is used it will not be regenerated, each ID is unique regardless if the data still exists or not. If IDs were recycled you could get into issues with foreign references that may have pointed to the old item with that ID and now would point to a new different record with the reused ID.

Typically you don't expose the IDs to the user anyway so it is a non issue.

書生途 2024-07-20 15:07:33

您不应该依赖于自动生成的 id 序列已排序或没有间隙。 正如其他人所指出的,您所看到的行为对于自动生成的 id 来说是完全正常的行为,否则您将需要跳过很多麻烦。 如果您需要按插入顺序对 id 进行排序,则应放入自动生成的日期/时间字段,然后选择按该字段排序的数据(并为其建立索引)。 这样,如果您决定从数字 ID 切换到 GUID 或其他排序顺序与插入顺序不同的 ID 格式,您的数据仍然会正确排序。 如果您需要为每个订单“下订单”,请在选择按日期订购时自动生成该订单(例如行号)。 这样,即使记录稍后被删除,您仍然会有严格的数字排序。

You shouldn't depend on autogenerated ids sequences being ordered or not having gaps. As others have noted, the behavior you are seeing is perfectly normal behavior for an autogenerated id and to make it otherwise you'll need to jump through a lot of hoops. If you need the ids to be ordered by the insertion sequence, you should put in an autogenerated date/time field and then select the data ordered by that field (and index it). That way if you ever decide to switch from a numeric id to a GUID or some other id format in which the sort order is different than the insertion order your data will still be ordered correctly. If you need to have a "place order" for each, generate that automatically (say a rownumber) as you are selecting ordered by date. That way you will still have strict numerical ordering even if records get deleted later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文