?? selfacnum.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication4
{
/// <summary>
/// SelFacNum 的摘要說明。
/// </summary>
public class SelFacNum : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textNum;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Label label2;
private System.Data.SqlClient.SqlDataAdapter facNumAdapter;
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.ComponentModel.IContainer components;
private System.Windows.Forms.Button sel_button;
private System.Windows.Forms.TextBox textName;
private DataTable facNumInfo=new DataTable();
public SelFacNum()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.sel_button = new System.Windows.Forms.Button();
this.textNum = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.facNumAdapter = 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.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textName);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.sel_button);
this.groupBox1.Controls.Add(this.textNum);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(16, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(400, 72);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "檢索項(xiàng)";
//
// textName
//
this.textName.Location = new System.Drawing.Point(136, 48);
this.textName.Name = "textName";
this.textName.Size = new System.Drawing.Size(88, 21);
this.textName.TabIndex = 4;
this.textName.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(104, 16);
this.label2.TabIndex = 3;
this.label2.Text = "按設(shè)備名稱檢索:";
//
// sel_button
//
this.sel_button.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.sel_button.Location = new System.Drawing.Point(288, 32);
this.sel_button.Name = "sel_button";
this.sel_button.Size = new System.Drawing.Size(80, 24);
this.sel_button.TabIndex = 2;
this.sel_button.Text = "檢索";
this.sel_button.Click += new System.EventHandler(this.sel_button_Click);
//
// textNum
//
this.textNum.Location = new System.Drawing.Point(136, 16);
this.textNum.Name = "textNum";
this.textNum.Size = new System.Drawing.Size(88, 21);
this.textNum.TabIndex = 1;
this.textNum.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 24);
this.label1.TabIndex = 0;
this.label1.Text = "按設(shè)備號檢索:";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 88);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(400, 200);
this.dataGrid1.TabIndex = 1;
//
// facNumAdapter
//
this.facNumAdapter.DeleteCommand = this.sqlDeleteCommand1;
this.facNumAdapter.InsertCommand = this.sqlInsertCommand1;
this.facNumAdapter.SelectCommand = this.sqlSelectCommand1;
this.facNumAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "facilityNum", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("設(shè)備號", "設(shè)備號"),
new System.Data.Common.DataColumnMapping("設(shè)備名稱", "設(shè)備名稱")})});
this.facNumAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM facilityNum WHERE (設(shè)備號 = @Original_設(shè)備號) AND (設(shè)備名稱 = @Original_設(shè)備名稱 OR" +
" @Original_設(shè)備名稱 IS NULL AND 設(shè)備名稱 IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備號", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備號", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備名稱", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=727CE26111204C4;packet size=4096;user id=sa;data source=\".\";persis" +
"t security info=False;initial catalog=storage";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO facilityNum(設(shè)備號, 設(shè)備名稱) VALUES (@設(shè)備號, @設(shè)備名稱); SELECT 設(shè)備號, 設(shè)備名稱 FROM fa" +
"cilityNum WHERE (設(shè)備號 = @設(shè)備號)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備號", System.Data.SqlDbType.VarChar, 10, "設(shè)備號"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, "設(shè)備名稱"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 設(shè)備號, 設(shè)備名稱 FROM facilityNum";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE facilityNum SET 設(shè)備號 = @設(shè)備號, 設(shè)備名稱 = @設(shè)備名稱 WHERE (設(shè)備號 = @Original_設(shè)備號) AND (" +
"設(shè)備名稱 = @Original_設(shè)備名稱 OR @Original_設(shè)備名稱 IS NULL AND 設(shè)備名稱 IS NULL); SELECT 設(shè)備號, 設(shè)" +
"備名稱 FROM facilityNum WHERE (設(shè)備號 = @設(shè)備號)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備號", System.Data.SqlDbType.VarChar, 10, "設(shè)備號"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, "設(shè)備名稱"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備號", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備號", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設(shè)備名稱", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設(shè)備名稱", System.Data.DataRowVersion.Original, null));
//
// SelFacNum
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 302);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.groupBox1);
this.Name = "SelFacNum";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "設(shè)備查詢";
this.Load += new System.EventHandler(this.SelFacNum_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void SelFacNum_Load(object sender, System.EventArgs e)
{
try
{
this.sqlConnection1.ConnectionString= "server=.;Trusted_Connection=yes;user=sa;pwd=1;database=storage";//建立連接
this.sqlConnection1.Open();
DataSet facNumSet=new DataSet();
facNumAdapter.SelectCommand.CommandText="SELECT * FROM facilityNum";
facNumAdapter.SelectCommand.Connection=sqlConnection1;
facNumAdapter.Fill(facNumInfo);
DataGridTableStyle fn= new DataGridTableStyle();//定義DataGrid格式
fn.AlternatingBackColor = Color.Blue;//顏色設(shè)置
fn.MappingName = facNumInfo.TableName;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void sel_button_Click(object sender, System.EventArgs e)
{
string condition = "";
if(this.textNum.Text.Trim() != "")//設(shè)置表的過濾條件
{
condition += "設(shè)備號 like '%" + textNum.Text.Trim() + "%'";
if(this.textName.Text.Trim() != "")
{
condition += "and 設(shè)備名稱 like '%" + textName.Text.Trim() + "%" + "'";
}
}
else
{
if(this.textName.Text.Trim()!="")
{
condition += "設(shè)備名稱 like '%" + textName.Text.Trim() + "%'";
}
else
{
MessageBox.Show("請輸入查詢條件");
return;
}
}
facNumInfo.DefaultView.RowFilter=condition;
this.dataGrid1.DataSource = facNumInfo.DefaultView;//設(shè)置數(shù)據(jù)源
if(this.dataGrid1.VisibleRowCount==1)//判斷檢索條件是否與記錄匹配
{
MessageBox.Show("對不起,出庫數(shù)據(jù)中沒有與您檢索條件相匹配的記錄!");
return;
}
this.textNum.Clear();//清空字段
this.textName.Clear();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -