?? seldriveinfo.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>
/// SelDriveInfo 的摘要說明。
/// </summary>
public class SelDriveInfo : 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.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.SqlClient.SqlDataAdapter driveAdapter;
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 DataTable driveInfo=new DataTable();
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public SelDriveInfo()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textNum = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.driveAdapter = 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.button1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.textNum);
this.groupBox1.Location = new System.Drawing.Point(216, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 248);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "檢索項";
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.Location = new System.Drawing.Point(56, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 2;
this.button1.Text = "檢索";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 16);
this.label1.TabIndex = 0;
this.label1.Text = "按設備號檢索:";
//
// textNum
//
this.textNum.Location = new System.Drawing.Point(48, 80);
this.textNum.Name = "textNum";
this.textNum.Size = new System.Drawing.Size(88, 21);
this.textNum.TabIndex = 1;
this.textNum.Text = "";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(40, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(168, 248);
this.dataGrid1.TabIndex = 1;
//
// driveAdapter
//
this.driveAdapter.DeleteCommand = this.sqlDeleteCommand1;
this.driveAdapter.InsertCommand = this.sqlInsertCommand1;
this.driveAdapter.SelectCommand = this.sqlSelectCommand1;
this.driveAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "drivestorage", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("設備號", "設備號"),
new System.Data.Common.DataColumnMapping("現有庫存量", "現有庫存量")})});
this.driveAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM drivestorage WHERE (設備號 = @Original_設備號) AND (現有庫存量 = @Original_現有庫存量" +
" OR @Original_現有庫存量 IS NULL AND 現有庫存量 IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設備號", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設備號", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_現有庫存量", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "現有庫存量", 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 drivestorage(設備號, 現有庫存量) VALUES (@設備號, @現有庫存量); SELECT 設備號, 現有庫存量 FRO" +
"M drivestorage WHERE (設備號 = @設備號)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設備號", System.Data.SqlDbType.VarChar, 10, "設備號"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@現有庫存量", System.Data.SqlDbType.VarChar, 10, "現有庫存量"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 設備號, 現有庫存量 FROM drivestorage";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE drivestorage SET 設備號 = @設備號, 現有庫存量 = @現有庫存量 WHERE (設備號 = @Original_設備號) AN" +
"D (現有庫存量 = @Original_現有庫存量 OR @Original_現有庫存量 IS NULL AND 現有庫存量 IS NULL); SELECT" +
" 設備號, 現有庫存量 FROM drivestorage WHERE (設備號 = @設備號)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@設備號", System.Data.SqlDbType.VarChar, 10, "設備號"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@現有庫存量", System.Data.SqlDbType.VarChar, 10, "現有庫存量"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_設備號", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "設備號", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_現有庫存量", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "現有庫存量", System.Data.DataRowVersion.Original, null));
//
// SelDriveInfo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(424, 294);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.groupBox1);
this.Name = "SelDriveInfo";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "庫存量查詢";
this.Load += new System.EventHandler(this.SelDriveInfo_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void SelDriveInfo_Load(object sender, System.EventArgs e)
{
try
{
this.sqlConnection1.ConnectionString= "server=.;Trusted_Connection=yes;user=sa;pwd=1;database=storage";//建立連接
this.sqlConnection1.Open();
DataSet driveSet=new DataSet();
driveAdapter.SelectCommand.CommandText="SELECT * FROM drivestorage";
driveAdapter.SelectCommand.Connection=sqlConnection1;
driveAdapter.Fill(driveInfo);
DataGridTableStyle dr = new DataGridTableStyle();
dr.AlternatingBackColor = Color.Blue;//顏色設置
dr.MappingName = driveInfo.TableName;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void button1_Click(object sender, System.EventArgs e)
{
string condition = "";
if(this.textNum.Text.Trim() != "")//設置表的過濾條件
condition += "設備號 like '%" + textNum.Text.Trim() + "%'";
else
{
MessageBox.Show("請輸入查詢條件");
return;
}
driveInfo.DefaultView.RowFilter=condition;
this.dataGrid1.DataSource = driveInfo.DefaultView;//設置數據源
if(this.dataGrid1.VisibleRowCount==1)//判斷檢索條件是否與記錄匹配
{
MessageBox.Show("對不起,出庫數據中沒有與您檢索條件相匹配的記錄!");
return;
}
this.textNum.Clear();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -