使用C#的Windows窗体,通过button点击,将SQL的test数据库NJ$表中包含的数据数据全部删除。

该篇文章介绍了如何使用C#的Windows窗体程序,通过按钮点击事件,连接到test数据库并执行SQL命令,删除NJ$表中的所有数据,同时保持表结构不变。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用C#的Windows窗体,通过button点击,将SQL的test数据库NJ$表中包含的数据数据全部删除,只删除数据,保留表结构。

实现代码:

using System;
using System.Data.SqlClient;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// 连接字符串,这里假设你的数据库在localhost上,数据库名为test,用户名为username,密码为password
string connectionString = "Server=localhost;Database=test;User Id=username;Password=password;";

// 使用SqlConnection类连接到数据库
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
// 打开数据库连接
connection.Open();

// SQL命令,删除NJ$表中的所有数据
string sql = "DELETE FROM NJ$";

// 使用SqlCommand类执行SQL命令
using (SqlCommand command = new SqlCommand(sql, connection))
{
// 执行SQL命令
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// 如果出现错误,显示错误信息
Console.WriteLine(ex.Message);
}
}
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值