📄 form_modifyitem.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_ModifyItem 的摘要说明。
/// </summary>
public class Form_ModifyItem : 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.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private 数据库更新.DataSet_ModifyItem dataSet_ModifyItem1;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form_ModifyItem()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <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.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet_ModifyItem1 = new 数据库更新.DataSet_ModifyItem();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataSet_ModifyItem1)).BeginInit();
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(40, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(216, 23);
this.label1.TabIndex = 11;
this.label1.Text = "修改数据库中的一个记录";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button3
//
this.button3.Location = new System.Drawing.Point(38, 200);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(224, 23);
this.button3.TabIndex = 10;
this.button3.Text = "利用数据集更新数据库(工具箱控件)";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(38, 168);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(224, 23);
this.button2.TabIndex = 9;
this.button2.Text = "利用数据集更新数据库(手工代码)";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(38, 136);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(224, 23);
this.button1.TabIndex = 8;
this.button1.Text = "利用SQL语句更新数据库";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(38, 88);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(224, 21);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "请输入类别描述(可以为空)";
this.textBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox2_MouseDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(38, 48);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "请输入类别名称(不能为空)";
this.textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Categories", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("CategoryName", "CategoryName"),
new System.Data.Common.DataColumnMapping("Description", "Description"),
new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM Categories WHERE (CategoryID = @Original_CategoryID) AND (CategoryNam" +
"e = @Original_CategoryName)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=HZX;packet size=4096;integrated security=SSPI;initial catalog=Nort" +
"hwind;persist security info=False";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Categories(CategoryName, Description) VALUES (@CategoryName, @Descrip" +
"tion); SELECT CategoryName, Description, CategoryID FROM Categories WHERE (Categ" +
"oryID = @@IDENTITY)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT CategoryName, Description, CategoryID FROM Categories WHERE (CategoryID = " +
"9)";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE Categories SET CategoryName = @CategoryName, Description = @Description WHERE (CategoryID = @Original_CategoryID) AND (CategoryName = @Original_CategoryName); SELECT CategoryName, Description, CategoryID FROM Categories WHERE (CategoryID = @CategoryID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"));
//
// dataSet_ModifyItem1
//
this.dataSet_ModifyItem1.DataSetName = "DataSet_ModifyItem";
this.dataSet_ModifyItem1.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// button4
//
this.button4.Location = new System.Drawing.Point(104, 232);
this.button4.Name = "button4";
this.button4.TabIndex = 12;
this.button4.Text = "返回";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form_ModifyItem
//
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.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form_ModifyItem";
this.Text = "Form_ModifyItem";
((System.ComponentModel.ISupportInitialize)(this.dataSet_ModifyItem1)).EndInit();
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;
connection.Open();
command.CommandText = "UPDATE Categories SET CategoryName = '" + this.textBox1.Text + "'," + "Description = '" + this.textBox2.Text + "' WHERE CategoryID=9";
try
{
command.ExecuteNonQuery();
MessageBox.Show("数据库记录修改成功","修改记录");
}
catch(SqlException x)
{
MessageBox.Show("数据库记录修改失败","修改记录");
}
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=9";
command.Connection = connection;
connection.Open();
adapter.SelectCommand = command;
adapter.Fill(ds,"Categories");
table = ds.Tables["Categories"];
DataRow row = table.Rows[0];
row["CategoryName"] = this.textBox1.Text;
row["Description"] = this.textBox2.Text;
try
{
adapter.Update(ds,"Categories");
MessageBox.Show("数据库记录修改成功","修改记录");
}
catch(SqlException x)
{
MessageBox.Show("数据库记录修改失败","修改记录");
}
connection.Close();
}
private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.textBox1.Text = "";
}
private void textBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.textBox2.Text = "";
}
private void button3_Click(object sender, System.EventArgs e)
{
this.sqlConnection1.Open();
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.sqlDataAdapter1.Fill(this.dataSet_ModifyItem1,"Categories");
DataTable table = this.dataSet_ModifyItem1.Tables["Categories"];
DataRow row = table.Rows[0];
row["CategoryName"] = this.textBox1.Text;
row["Description"] = this.textBox2.Text;
try
{
this.sqlDataAdapter1.Update(this.dataSet_ModifyItem1,"Categories");
MessageBox.Show("数据库记录修改成功","修改记录");
}
catch(SqlException x)
{
MessageBox.Show("数据库记录修改失败","修改记录");
}
this.sqlConnection1.Close();
}
private void button4_Click(object sender, System.EventArgs e)
{
new Form1().Show();
this.Dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -