📄 form_deleteitem.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace 数据库更新
{
/// <summary>
/// Form_DeleteItem 的摘要说明。
/// </summary>
public class Form_DeleteItem : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form_DeleteItem()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand();
connection.ConnectionString = "workstation id=localhost;packet size=4096;integrated security=SSPI;initial catalog=Northwind;persist security info=False";
command.CommandText = "SELECT * FROM Categories";
command.Connection = connection;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
this.comboBox1.Items.Add(reader[0].ToString());
}
connection.Close();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(36, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(216, 23);
this.label1.TabIndex = 17;
this.label1.Text = "删除数据库中的一个记录";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button3
//
this.button3.Location = new System.Drawing.Point(34, 200);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(224, 23);
this.button3.TabIndex = 16;
this.button3.Text = "利用数据集更新数据库(工具箱控件)";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(34, 168);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(224, 23);
this.button2.TabIndex = 15;
this.button2.Text = "利用数据集更新数据库(手工代码)";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(34, 136);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(224, 23);
this.button1.TabIndex = 14;
this.button1.Text = "利用SQL语句更新数据库";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(80, 96);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 18;
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(224, 23);
this.label2.TabIndex = 19;
this.label2.Text = "选择要删除的记录的ID号";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button4
//
this.button4.Location = new System.Drawing.Point(112, 232);
this.button4.Name = "button4";
this.button4.TabIndex = 20;
this.button4.Text = "返回";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form_DeleteItem
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 262);
this.Controls.Add(this.button4);
this.Controls.Add(this.label2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form_DeleteItem";
this.Text = "Form_DeleteItem";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand();
connection.ConnectionString = "workstation id=localhost;packet size=4096;integrated security=SSPI;initial catalog=Northwind;persist security info=False";
command.Connection = connection;
if(this.comboBox1.SelectedIndex != -1)
{
connection.Open();
command.CommandText = "DELETE FROM Categories WHERE CategoryID=" + this.comboBox1.SelectedItem.ToString();
try
{
command.ExecuteNonQuery();
MessageBox.Show("数据库记录删除成功","删除记录");
}
catch(SqlException x)
{
MessageBox.Show("数据库记录删除失败","删除记录");
}
connection.Close();
}
else
{
MessageBox.Show("请选择要删除的记录的ID号","删除记录");
}
//将列表框中记录的ID号刷新,去掉已经删掉的记录
this.comboBox1.SelectedIndex = -1;
this.comboBox1.Items.Clear();
SqlCommand command1 = new SqlCommand();
command1.CommandText = "SELECT * FROM Categories";
command1.Connection = connection;
connection.Open();
SqlDataReader reader = command1.ExecuteReader();
while(reader.Read())
{
this.comboBox1.Items.Add(reader[0].ToString());
}
connection.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
SqlDataAdapter adapter = new SqlDataAdapter();
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand();
DataSet ds = new DataSet();
DataTable table = new DataTable();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter);
connection.ConnectionString = "workstation id=localhost;packet size=4096;integrated security=SSPI;initial catalog=Northwind;persist security info=False";
command.CommandText = "SELECT * FROM Categories WHERE CategoryID=" + this.comboBox1.SelectedItem.ToString();
command.Connection = connection;
connection.Open();
adapter.SelectCommand = command;
adapter.Fill(ds,"Categories");
table = ds.Tables["Categories"];
DataRow row = table.Rows[0];
table.Rows[0].Delete();
try
{
adapter.Update(ds,"Categories");
MessageBox.Show("数据库记录删除成功","删除记录");
}
catch(SqlException x)
{
MessageBox.Show("数据库记录删除失败","删除记录");
}
connection.Close();
//将列表框中记录的ID号刷新,去掉已经删掉的记录
this.comboBox1.SelectedIndex = -1;
this.comboBox1.Items.Clear();
SqlCommand command1 = new SqlCommand();
command1.CommandText = "SELECT * FROM Categories";
command1.Connection = connection;
connection.Open();
SqlDataReader reader = command1.ExecuteReader();
while(reader.Read())
{
this.comboBox1.Items.Add(reader[0].ToString());
}
connection.Close();
}
private void button4_Click(object sender, System.EventArgs e)
{
new Form1().Show();
this.Dispose();
}
private void button3_Click(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -