14.2.txt

来自「《Microsoft Visual C# .NET 2003开发技巧大全》源代码」· 文本 代码 · 共 80 行

TXT
80
字号
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 + =
减小字号Ctrl + -
显示快捷键?