📄 14.2.txt
字号:
Listing 14.2 Performing Database Updates
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace _8_DataBoundControls
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tbName;
private System.Windows.Forms.Button btnPrev;
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.Label label1;
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 System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private _8_DataBoundControls.ProductDS productDS1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
sqlDataAdapter1.Fill( productDS1 );
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// Windows Form Designer Generated Code
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnPrev_Click(object sender, System.EventArgs e)
{
tbName.BindingContext[productDS1, “Products”].Position -= 1;
}
private void btnNext_Click(object sender, System.EventArgs e)
{
this.BindingContext[productDS1, “Products”].Position += 1;
}
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if( productDS1.HasChanges() )
{
DialogResult result;
result = MessageBox.Show( this,
“Would you like to save your changes?”,
“Northwind Products”, MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question );
if( result == DialogResult.Cancel )
{
e.Cancel = true;
return;
}
else if( result == DialogResult.No )
{
return;
}
else
{
sqlDataAdapter1.Update( productDS1 );
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -