📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataBindSample
{
/// <summary>
/// Form1 的摘要说明
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private DataBindSample.dsAuthors dsAuthors1;
private System.Windows.Forms.TextBox txtStateParameter;
private System.Windows.Forms.TextBox txtAuthorID;
private System.Windows.Forms.TextBox txtAuthorLName;
private System.Windows.Forms.TextBox txtAuthorState;
private System.Windows.Forms.Button btnShow;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnPrevious;
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.TextBox txtPosition;
/// <summary>
/// 必需的设计器变量
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容
/// </summary>
private void InitializeComponent()
{
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.dsAuthors1 = new DataBindSample.dsAuthors();
this.txtStateParameter = new System.Windows.Forms.TextBox();
this.txtAuthorID = new System.Windows.Forms.TextBox();
this.txtAuthorLName = new System.Windows.Forms.TextBox();
this.txtAuthorState = new System.Windows.Forms.TextBox();
this.btnShow = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.btnPrevious = new System.Windows.Forms.Button();
this.btnNext = new System.Windows.Forms.Button();
this.txtPosition = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.dsAuthors1)).BeginInit();
this.SuspendLayout();
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
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", "authors", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("au_id", "au_id"),
new System.Data.Common.DataColumnMapping("au_lname", "au_lname"),
new System.Data.Common.DataColumnMapping("state", "state")})});
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// oleDbDeleteCommand1
//
this.oleDbDeleteCommand1.CommandText = "DELETE FROM authors WHERE (au_id = ?) AND (au_lname = ?) AND (state = ? OR ? IS N" +
"ULL AND state IS NULL)";
this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_au_id", System.Data.OleDb.OleDbType.VarChar, 11, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "au_id", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_au_lname", System.Data.OleDb.OleDbType.VarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "au_lname", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_state", System.Data.OleDb.OleDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "state", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_state1", System.Data.OleDb.OleDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "state", System.Data.DataRowVersion.Original, null));
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = @"Provider=SQLOLEDB.1;Password=pass;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=CHENBIN;Use Encryption for Data=False;Tag with column collation when possible=False";
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.CommandText = "INSERT INTO authors(au_id, au_lname, state) VALUES (?, ?, ?); SELECT au_id, au_ln" +
"ame, state FROM authors WHERE (au_id = ?)";
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("au_id", System.Data.OleDb.OleDbType.VarChar, 11, "au_id"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("au_lname", System.Data.OleDb.OleDbType.VarChar, 40, "au_lname"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("state", System.Data.OleDb.OleDbType.VarChar, 2, "state"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Select_au_id", System.Data.OleDb.OleDbType.VarChar, 11, "au_id"));
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT au_id, au_lname, state FROM authors WHERE (state = ?)";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
this.oleDbSelectCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("state", System.Data.OleDb.OleDbType.VarChar, 2, "state"));
//
// oleDbUpdateCommand1
//
this.oleDbUpdateCommand1.CommandText = "UPDATE authors SET au_id = ?, au_lname = ?, state = ? WHERE (au_id = ?) AND (au_l" +
"name = ?) AND (state = ? OR ? IS NULL AND state IS NULL); SELECT au_id, au_lname" +
", state FROM authors WHERE (au_id = ?)";
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("au_id", System.Data.OleDb.OleDbType.VarChar, 11, "au_id"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("au_lname", System.Data.OleDb.OleDbType.VarChar, 40, "au_lname"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("state", System.Data.OleDb.OleDbType.VarChar, 2, "state"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_au_id", System.Data.OleDb.OleDbType.VarChar, 11, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "au_id", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_au_lname", System.Data.OleDb.OleDbType.VarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "au_lname", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_state", System.Data.OleDb.OleDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "state", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_state1", System.Data.OleDb.OleDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "state", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Select_au_id", System.Data.OleDb.OleDbType.VarChar, 11, "au_id"));
//
// dsAuthors1
//
this.dsAuthors1.DataSetName = "dsAuthors";
this.dsAuthors1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.dsAuthors1.Namespace = "http://www.tempuri.org/dsAuthors.xsd";
//
// txtStateParameter
//
this.txtStateParameter.Location = new System.Drawing.Point(96, 24);
this.txtStateParameter.Name = "txtStateParameter";
this.txtStateParameter.TabIndex = 0;
this.txtStateParameter.Text = "";
//
// txtAuthorID
//
this.txtAuthorID.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dsAuthors1, "authors.au_id"));
this.txtAuthorID.Location = new System.Drawing.Point(96, 80);
this.txtAuthorID.Name = "txtAuthorID";
this.txtAuthorID.TabIndex = 1;
this.txtAuthorID.Text = "";
//
// txtAuthorLName
//
this.txtAuthorLName.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dsAuthors1, "authors.au_lname"));
this.txtAuthorLName.Location = new System.Drawing.Point(96, 112);
this.txtAuthorLName.Name = "txtAuthorLName";
this.txtAuthorLName.TabIndex = 2;
this.txtAuthorLName.Text = "";
//
// txtAuthorState
//
this.txtAuthorState.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dsAuthors1, "authors.state"));
this.txtAuthorState.Location = new System.Drawing.Point(96, 144);
this.txtAuthorState.Name = "txtAuthorState";
this.txtAuthorState.TabIndex = 3;
this.txtAuthorState.Text = "";
//
// btnShow
//
this.btnShow.Location = new System.Drawing.Point(216, 24);
this.btnShow.Name = "btnShow";
this.btnShow.Size = new System.Drawing.Size(56, 23);
this.btnShow.TabIndex = 4;
this.btnShow.Text = "显示";
this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 23);
this.label1.TabIndex = 5;
this.label1.Text = "请输入州代码:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 6;
this.label2.Text = "作者 ID:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(48, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(32, 23);
this.label3.TabIndex = 7;
this.label3.Text = "姓:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(48, 144);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(32, 23);
this.label4.TabIndex = 8;
this.label4.Text = "州:";
//
// btnPrevious
//
this.btnPrevious.Location = new System.Drawing.Point(32, 184);
this.btnPrevious.Name = "btnPrevious";
this.btnPrevious.Size = new System.Drawing.Size(56, 23);
this.btnPrevious.TabIndex = 9;
this.btnPrevious.Text = "上一步";
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
//
// btnNext
//
this.btnNext.Location = new System.Drawing.Point(208, 184);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(56, 23);
this.btnNext.TabIndex = 10;
this.btnNext.Text = "下一步";
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// txtPosition
//
this.txtPosition.Enabled = false;
this.txtPosition.Location = new System.Drawing.Point(96, 184);
this.txtPosition.Name = "txtPosition";
this.txtPosition.TabIndex = 11;
this.txtPosition.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 229);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.txtPosition,
this.btnNext,
this.btnPrevious,
this.label4,
this.label3,
this.label2,
this.label1,
this.btnShow,
this.txtAuthorState,
this.txtAuthorLName,
this.txtAuthorID,
this.txtStateParameter});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dsAuthors1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnShow_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.SelectCommand.Parameters["state"].Value = txtStateParameter.Text;
dsAuthors1.Clear();
oleDbDataAdapter1.Fill(dsAuthors1);
ShowPosition();
}
private void btnPrevious_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsAuthors1, "authors"].Position -=1 ;
ShowPosition();
}
private void btnNext_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsAuthors1, "authors"].Position +=1 ;
ShowPosition();
}
private void ShowPosition()
{
int iCnt;
int iPos;
iCnt = this.BindingContext[dsAuthors1, "authors"].Count;
iPos = this.BindingContext[dsAuthors1, "authors"].Position + 1;
if(iCnt == 0)
{
txtPosition.Text = "(No records)";
}
else
{
txtPosition.Text = iPos.ToString() + " of " + iCnt.ToString() ;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -