C# 中的 DELETE 语句可从表中删除数据。语法:DELETE FROM table_name[WHERE condition]。步骤:创建 SqlCommand 对象并指定语句和连接;调用 ExecuteNonQuery 方法执行语句。

C# 中的 DELETE 语句
DELETE 语句用于从数据库表中删除一行或多行数据。它的语法如下:
<code class="csharp">DELETE FROM table_name [WHERE condition];</code>
参数:
执行 DELTE 语句
要执行 DELETE 语句,可以使用以下步骤:
SqlCommand 对象,指定 DELETE 语句和要执行它的数据库连接。SqlCommand 的 ExecuteNonQuery 方法来执行 DELETE 语句。示例
以下示例删除表 Customers 中 ID 为 10 的行:
<code class="csharp">using System.Data.SqlClient;
public static void DeleteCustomer(int customerId)
{
// 创建数据库连接
SqlConnection connection = new SqlConnection(connectionString);
// 创建 DELETE 语句
SqlCommand command = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// 添加参数
command.Parameters.AddWithValue("@CustomerID", customerId);
// 打开连接并执行 DELETE 语句
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}</code>以上就是c#删除语句怎么写的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号