asp.net检查是插入表之前存在的主键

时间:2022-08-01 15:40:31

looking for a simple way to check if a primary key already exists in the table before inserting into it. where Fname is the primary key in the table.

在插入之前,寻找一种简单的方法来检查表中是否已存在主键。其中Fname是表中的主键。

SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO [table] ([firstName], [lastName]) VALUES ('" + txtFName.Text + "','" + txtLName.Text + "')";        

cmd.ExecuteNonQuery();
conn.Close();

1 个解决方案

#1


2  

As a general rule, you should just try to insert. The dbms will return an error if they key exists. Trap that error.

作为一般规则,您应该尝试插入。如果密钥存在,则dbms将返回错误。陷阱那个错误。

You have to trap errors anyway, because there are a lot of things besides primary key constraints that can prevent a row from being inserted. Among them

无论如何,你必须捕获错误,因为除了主键约束之外还有很多东西可以防止插入行。其中

  • Any other kind of constraint, like a foreign key constraint, a check constraint, etc.
  • 任何其他类型的约束,如外键约束,检查约束等。
  • Disk error.
  • 磁盘错误。
  • Network error.
  • 网络错误。
  • Hurricane.
  • 飓风。

You don't lose any efficiency by trapping the error.

通过捕获错误,您不会失去任何效率。

  • Inserting (no error) requires one round-trip to the database.
  • 插入(无错误)需要一次往返数据库。
  • Inserting (with error, which you trap) requires one round-trip to the database, not counting whatever you might have to do to correct that error. You'd have to do all the correcting if you checked first anyway.
  • 插入(带有错误,您捕获)需要一次往返数据库,不计算您可能需要做的任何事情来纠正该错误。如果你先检查了,你必须做所有纠正。
  • Check-then-insert requires two round-trips to the database.
  • Check-then-insert需要两次往返数据库。

Some platforms can merge data if you try to insert a row for which a primary key already exists. The most famous (or most notorious, depending on your point of view) is MySQL's ON DUPLICATE KEY UPDATE.

如果您尝试插入已存在主键的行,则某些平台可以合并数据。最着名的(或最臭名昭着的,取决于你的观点)是MySQL的ON DUPLICATE KEY UPDATE。

#1


2  

As a general rule, you should just try to insert. The dbms will return an error if they key exists. Trap that error.

作为一般规则,您应该尝试插入。如果密钥存在,则dbms将返回错误。陷阱那个错误。

You have to trap errors anyway, because there are a lot of things besides primary key constraints that can prevent a row from being inserted. Among them

无论如何,你必须捕获错误,因为除了主键约束之外还有很多东西可以防止插入行。其中

  • Any other kind of constraint, like a foreign key constraint, a check constraint, etc.
  • 任何其他类型的约束,如外键约束,检查约束等。
  • Disk error.
  • 磁盘错误。
  • Network error.
  • 网络错误。
  • Hurricane.
  • 飓风。

You don't lose any efficiency by trapping the error.

通过捕获错误,您不会失去任何效率。

  • Inserting (no error) requires one round-trip to the database.
  • 插入(无错误)需要一次往返数据库。
  • Inserting (with error, which you trap) requires one round-trip to the database, not counting whatever you might have to do to correct that error. You'd have to do all the correcting if you checked first anyway.
  • 插入(带有错误,您捕获)需要一次往返数据库,不计算您可能需要做的任何事情来纠正该错误。如果你先检查了,你必须做所有纠正。
  • Check-then-insert requires two round-trips to the database.
  • Check-then-insert需要两次往返数据库。

Some platforms can merge data if you try to insert a row for which a primary key already exists. The most famous (or most notorious, depending on your point of view) is MySQL's ON DUPLICATE KEY UPDATE.

如果您尝试插入已存在主键的行,则某些平台可以合并数据。最着名的(或最臭名昭着的,取决于你的观点)是MySQL的ON DUPLICATE KEY UPDATE。