fclsdataform.cs
来自「Sams Teach Yourself C# Web Programming i」· CS 代码 · 共 313 行
CS
313 行
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Data_Form_Example
{
/// <summary>
/// Summary description for fclsDataForm.
/// </summary>
public class fclsDataForm : System.Windows.Forms.Form
{
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private Data_Form_Example.AllContacts objAllContacts;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Button btnCancelAll;
private System.Windows.Forms.DataGrid grdContacts;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public fclsDataForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.objAllContacts = new Data_Form_Example.AllContacts();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.btnLoad = new System.Windows.Forms.Button();
this.btnUpdate = new System.Windows.Forms.Button();
this.btnCancelAll = new System.Windows.Forms.Button();
this.grdContacts = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.objAllContacts)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.grdContacts)).BeginInit();
this.SuspendLayout();
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT ContactName, State FROM Contacts";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.CommandText = "INSERT INTO Contacts(ContactName, State) VALUES (?, ?)";
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("ContactName", System.Data.OleDb.OleDbType.VarWChar, 255, "ContactName"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("State", System.Data.OleDb.OleDbType.VarWChar, 50, "State"));
//
// objAllContacts
//
this.objAllContacts.DataSetName = "AllContacts";
this.objAllContacts.Locale = new System.Globalization.CultureInfo("en-US");
this.objAllContacts.Namespace = "http://www.tempuri.org/AllContacts.xsd";
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Contacts.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Contacts", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ContactName", "ContactName"),
new System.Data.Common.DataColumnMapping("State", "State")})});
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(10, 10);
this.btnLoad.Name = "btnLoad";
this.btnLoad.TabIndex = 0;
this.btnLoad.Text = "&Load";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// btnUpdate
//
this.btnUpdate.Location = new System.Drawing.Point(315, 10);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.TabIndex = 1;
this.btnUpdate.Text = "&Update";
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// btnCancelAll
//
this.btnCancelAll.Location = new System.Drawing.Point(315, 43);
this.btnCancelAll.Name = "btnCancelAll";
this.btnCancelAll.TabIndex = 2;
this.btnCancelAll.Text = "Ca&ncel All";
this.btnCancelAll.Click += new System.EventHandler(this.btnCancelAll_Click);
//
// grdContacts
//
this.grdContacts.DataMember = "Contacts";
this.grdContacts.DataSource = this.objAllContacts;
this.grdContacts.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grdContacts.Location = new System.Drawing.Point(10, 76);
this.grdContacts.Name = "grdContacts";
this.grdContacts.Size = new System.Drawing.Size(380, 164);
this.grdContacts.TabIndex = 3;
//
// fclsDataForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 223);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnLoad,
this.btnUpdate,
this.btnCancelAll,
this.grdContacts});
this.Name = "fclsDataForm";
this.Text = "fclsDataForm";
((System.ComponentModel.ISupportInitialize)(this.objAllContacts)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.grdContacts)).EndInit();
this.ResumeLayout(false);
}
#endregion
public void FillDataSet(Data_Form_Example.AllContacts dataSet)
{
// Turn off constraint checking before the dataset is filled.
// This allows the adapters to fill the dataset without concern
// for dependencies between the tables.
dataSet.EnforceConstraints = false;
try
{
// Open the connection.
this.oleDbConnection1.Open();
// Attempt to fill the dataset through the OleDbDataAdapter1.
this.oleDbDataAdapter1.Fill(dataSet);
}
catch (System.Exception fillException)
{
// Add your error handling code here.
throw fillException;
}
finally
{
// Turn constraint checking back on.
dataSet.EnforceConstraints = true;
// Close the connection whether or not the exception was thrown.
this.oleDbConnection1.Close();
}
}
public void UpdateDataSource(Data_Form_Example.AllContacts ChangedRows)
{
try
{
// The data source only needs to be updated if there are changes pending.
if ((ChangedRows != null))
{
// Open the connection.
this.oleDbConnection1.Open();
// Attempt to update the data source.
oleDbDataAdapter1.Update(ChangedRows);
}
}
catch (System.Exception updateException)
{
// Add your error handling code here.
throw updateException;
}
finally
{
// Close the connection whether or not the exception was thrown.
this.oleDbConnection1.Close();
}
}
public void LoadDataSet()
{
// Create a new dataset to hold the records returned from the call to FillDataSet.
// A temporary dataset is used because filling the existing dataset would
// require the databindings to be rebound.
Data_Form_Example.AllContacts objDataSetTemp;
objDataSetTemp = new Data_Form_Example.AllContacts();
try
{
// Attempt to fill the temporary dataset.
this.FillDataSet(objDataSetTemp);
}
catch (System.Exception eFillDataSet)
{
// Add your error handling code here.
throw eFillDataSet;
}
try
{
// Empty the old records from the dataset.
objAllContacts.Clear();
// Merge the records into the main dataset.
objAllContacts.Merge(objDataSetTemp);
}
catch (System.Exception eLoadMerge)
{
// Add your error handling code here.
throw eLoadMerge;
}
}
public void UpdateDataSet()
{
// Create a new dataset to hold the changes that have been made to the main dataset.
Data_Form_Example.AllContacts objDataSetChanges = new Data_Form_Example.AllContacts();
// Stop any current edits.
this.BindingContext[objAllContacts,"Contacts"].EndCurrentEdit();
// Get the changes that have been made to the main dataset.
objDataSetChanges = ((Data_Form_Example.AllContacts)(objAllContacts.GetChanges()));
// Check to see if any changes have been made.
if ((objDataSetChanges != null))
{
try
{
// There are changes that need to be made, so attempt to update the datasource by
// calling the update method and passing the dataset and any parameters.
this.UpdateDataSource(objDataSetChanges);
objAllContacts.Merge(objDataSetChanges);
objAllContacts.AcceptChanges();
}
catch (System.Exception eUpdate)
{
// Add your error handling code here.
throw eUpdate;
}
// Add your code to check the returned dataset for any errors that may have been
// pushed into the row object's error.
}
}
private void btnCancelAll_Click(object sender, System.EventArgs e)
{
this.objAllContacts.RejectChanges();
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
try
{
// Attempt to load the dataset.
this.LoadDataSet();
}
catch (System.Exception eLoad)
{
// Add your error handling code here.
// Display error message, if any.
System.Windows.Forms.MessageBox.Show(eLoad.Message);
}
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{
try
{
// Attempt to update the datasource.
this.UpdateDataSet();
}
catch (System.Exception eUpdate)
{
// Add your error handling code here.
// Display error message, if any.
System.Windows.Forms.MessageBox.Show(eUpdate.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?