⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 query.cs

📁 《C#数据库项目案例导航》一书的配套光盘
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Sale.UILevel
{
	/// <summary>
	/// Query 的摘要说明。
	/// </summary>
	public class Query : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		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 btnQuery;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Button btnClear;
		private System.Windows.Forms.TextBox textValue;
		private System.Windows.Forms.ComboBox comboDataItem;
		private System.Windows.Forms.ComboBox comboDataTable;
		private System.Windows.Forms.ComboBox comboCondition;
		private System.Data.SqlClient.SqlConnection sqlConnection1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Query()
		{
			//
			// 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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Query));
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.comboDataTable = new System.Windows.Forms.ComboBox();
			this.comboDataItem = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.textValue = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.btnQuery = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.btnClear = new System.Windows.Forms.Button();
			this.comboCondition = new System.Windows.Forms.ComboBox();
			this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.AlternatingBackColor = System.Drawing.Color.GhostWhite;
			this.dataGrid1.BackColor = System.Drawing.Color.GhostWhite;
			this.dataGrid1.BackgroundColor = System.Drawing.Color.Lavender;
			this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid1.CaptionBackColor = System.Drawing.Color.RoyalBlue;
			this.dataGrid1.CaptionForeColor = System.Drawing.Color.White;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.FlatMode = true;
			this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
			this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.GridLineColor = System.Drawing.Color.RoyalBlue;
			this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
			this.dataGrid1.HeaderForeColor = System.Drawing.Color.Lavender;
			this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
			this.dataGrid1.Location = new System.Drawing.Point(24, 136);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Lavender;
			this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.SelectionBackColor = System.Drawing.Color.Teal;
			this.dataGrid1.SelectionForeColor = System.Drawing.Color.PaleGreen;
			this.dataGrid1.Size = new System.Drawing.Size(440, 224);
			this.dataGrid1.TabIndex = 0;
			// 
			// comboDataTable
			// 
			this.comboDataTable.Items.AddRange(new object[] {
																"-请选择-",
																"Provider",
																"Client",
																"Product",
																"ProBuy",
																"ProSell",
																"ProStock"});
			this.comboDataTable.Location = new System.Drawing.Point(24, 32);
			this.comboDataTable.Name = "comboDataTable";
			this.comboDataTable.Size = new System.Drawing.Size(72, 20);
			this.comboDataTable.TabIndex = 1;
			this.comboDataTable.Text = "-请选择-";
			this.comboDataTable.SelectedIndexChanged += new System.EventHandler(this.comboDataTable_SelectedIndexChanged);
			// 
			// comboDataItem
			// 
			this.comboDataItem.Location = new System.Drawing.Point(24, 88);
			this.comboDataItem.Name = "comboDataItem";
			this.comboDataItem.Size = new System.Drawing.Size(72, 20);
			this.comboDataItem.TabIndex = 2;
			this.comboDataItem.Text = "-请选择-";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(32, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "数据表";
			// 
			// textValue
			// 
			this.textValue.Location = new System.Drawing.Point(200, 56);
			this.textValue.Name = "textValue";
			this.textValue.Size = new System.Drawing.Size(80, 21);
			this.textValue.TabIndex = 10;
			this.textValue.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(40, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 16);
			this.label2.TabIndex = 11;
			this.label2.Text = "数据项";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(216, 32);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(48, 16);
			this.label3.TabIndex = 12;
			this.label3.Text = "指定值";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(120, 32);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 13;
			this.label4.Text = "运算符";
			// 
			// btnQuery
			// 
			this.btnQuery.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnQuery.Image = ((System.Drawing.Image)(resources.GetObject("btnQuery.Image")));
			this.btnQuery.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnQuery.Location = new System.Drawing.Point(336, 64);
			this.btnQuery.Name = "btnQuery";
			this.btnQuery.Size = new System.Drawing.Size(64, 23);
			this.btnQuery.TabIndex = 16;
			this.btnQuery.Text = "查询";
			this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
			// 
			// btnExit
			// 
			this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image")));
			this.btnExit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnExit.Location = new System.Drawing.Point(336, 96);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(64, 24);
			this.btnExit.TabIndex = 15;
			this.btnExit.Text = "退出";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnClear
			// 
			this.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnClear.Image = ((System.Drawing.Image)(resources.GetObject("btnClear.Image")));
			this.btnClear.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnClear.Location = new System.Drawing.Point(336, 32);
			this.btnClear.Name = "btnClear";
			this.btnClear.Size = new System.Drawing.Size(64, 24);
			this.btnClear.TabIndex = 14;
			this.btnClear.Text = "重置";
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
			// 
			// comboCondition
			// 
			this.comboCondition.Items.AddRange(new object[] {
																"-请选择-",
																"=",
																">",
																">=",
																"<",
																"<=",
																"<>",
																"Like",
																"In"});
			this.comboCondition.Location = new System.Drawing.Point(112, 56);
			this.comboCondition.Name = "comboCondition";
			this.comboCondition.Size = new System.Drawing.Size(80, 20);
			this.comboCondition.TabIndex = 17;
			this.comboCondition.Text = "-请选择-";
			// 
			// Query
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(492, 393);
			this.Controls.Add(this.comboCondition);
			this.Controls.Add(this.btnQuery);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnClear);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textValue);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.comboDataItem);
			this.Controls.Add(this.comboDataTable);
			this.Controls.Add(this.dataGrid1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Query";
			this.Text = "综合查询";
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		public void ErrorHandle(System.Exception E)
		{
			MessageBox.Show(E.ToString());
		}		


		private void LoadDataSet()
		{
			DataSetQuery objDataSetTemp=new DataSetQuery();
			try
			{
				this.FillDataSet(objDataSetTemp);

			}
			catch (System.Exception E) 
			{
				// 在此处添加错误处理代码。
				this.ErrorHandle(E);
			}
			try
			{
				this.objDataSetQuery.Clear();
				this.objDataSetQuery.Merge(objDataSetTemp);
			}
			catch(System.Exception E) 
			{
				// 在此处添加错误处理代码。
				this.ErrorHandle(E);
			}
			
		}
		
		private void FillDataSet(DataSetQuery dataset)
		{
			dataset.EnforceConstraints=false;
			try
			{
				this.sqlConnection1.Open();
				this.sqlDataAdapterReader.Fill(dataset);
				this.sqlDataAdapterBook.Fill(dataset);					
				this.sqlDataAdapterPublish.Fill(dataset);
				this.sqlDataAdapterBorrow.Fill(dataset);

			}
			catch(System.Exception E)
			{
				this.ErrorHandle(E);
			}
		
			finally
			{
				dataset.EnforceConstraints=true;
				this.sqlConnection1.Close();
			}
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void comboDataTable_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string strTable=this.comboDataTable.SelectedItem.ToString();
			if(strTable=="") return;   
			this.comboDataItem.Text="";
			this.comboCondition.Text="";			
			this.textValue.Clear();
			string mySelectQuery="select * from "+strTable;
			SqlCommand myCommand=new SqlCommand(mySelectQuery,this.sqlConnection1);
			if(this.sqlConnection1.State==ConnectionState.Closed) this.sqlConnection1.Open();
			this.comboDataItem.Items.Clear();
			SqlDataReader myReader=myCommand.ExecuteReader();
			for(int i=0;i<myReader.FieldCount;i++)
			{
				this.comboDataItem.Items.Add(myReader.GetName(i).ToString());
			}
			myReader.Close();
			this.sqlConnection1.Close();			
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			this.comboDataTable.Text="";
			this.comboDataItem.Text="";
			this.comboCondition.Text="";
			this.textValue.Text="";

		}

		private void btnQuery_Click(object sender, System.EventArgs e)
		{
			SqlString="select * from "+this.comboDataTable.Text+" where ";
			SqlString=SqlString+this.comboDataItem.Text+this.comboCondition.Text+"'"+this.textValue.Text+"'";
			switch(this.comboDataTable.SelectedIndex)
			{
				case 0:
					this.sqlSelectCommand1.CommandText=SqlString;
					break;
				case 1:
					this.sqlSelectCommand2.CommandText=SqlString;
					break;
				case 2:
					this.sqlSelectCommand3.CommandText=SqlString;
					break;
				case 3:
					this.sqlSelectCommand4.CommandText=SqlString;
					break;
			}
			try
			{
				this.LoadDataSet();
				this.dataGrid1.DataSource=this.objDataSetQuery;
				this.dataGrid1.DataMember=this.comboDataTable.Text.Trim();
			}
			catch(System.Exception E)
			{
				this.ErrorHandle(E);
			}
		}

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -