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

📄 frmsearch.cs

📁 问题定义 随着生活水平的日益提高
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace 通讯录
{
	/// <summary>
	/// frmsearch 的摘要说明。
	/// </summary>
	public class frmsearch : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txtsearchvalue;
		private System.Windows.Forms.ComboBox cmbsearch2;
		private System.Windows.Forms.Button btnsearch;
		private System.Windows.Forms.ComboBox cmbsearch;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnexit;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		SqlConnection searchcon;
		DataSet ds=new DataSet();
		int j=0;//当前显示的记录序号
		int count=0;
		private System.Windows.Forms.DataGrid dataGrid1;//查询到的总共记录数目
		private System.ComponentModel.Container components = null;

		public frmsearch()
		{
			//
			// 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.txtsearchvalue = new System.Windows.Forms.TextBox();
			this.cmbsearch2 = new System.Windows.Forms.ComboBox();
			this.btnsearch = new System.Windows.Forms.Button();
			this.cmbsearch = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnexit = new System.Windows.Forms.Button();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txtsearchvalue);
			this.groupBox1.Controls.Add(this.cmbsearch2);
			this.groupBox1.Controls.Add(this.btnsearch);
			this.groupBox1.Controls.Add(this.cmbsearch);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
			this.groupBox1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.groupBox1.ForeColor = System.Drawing.Color.Red;
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(800, 72);
			this.groupBox1.TabIndex = 11;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "查询";
			// 
			// txtsearchvalue
			// 
			this.txtsearchvalue.ForeColor = System.Drawing.Color.Black;
			this.txtsearchvalue.Location = new System.Drawing.Point(408, 25);
			this.txtsearchvalue.Name = "txtsearchvalue";
			this.txtsearchvalue.Size = new System.Drawing.Size(152, 23);
			this.txtsearchvalue.TabIndex = 8;
			this.txtsearchvalue.Text = "";
			// 
			// cmbsearch2
			// 
			this.cmbsearch2.Items.AddRange(new object[] {
															"等于",
															"类似"});
			this.cmbsearch2.Location = new System.Drawing.Point(264, 26);
			this.cmbsearch2.Name = "cmbsearch2";
			this.cmbsearch2.Size = new System.Drawing.Size(104, 22);
			this.cmbsearch2.TabIndex = 7;
			this.cmbsearch2.Text = "等于";
			// 
			// btnsearch
			// 
			this.btnsearch.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnsearch.ForeColor = System.Drawing.Color.Black;
			this.btnsearch.Location = new System.Drawing.Point(600, 24);
			this.btnsearch.Name = "btnsearch";
			this.btnsearch.Size = new System.Drawing.Size(96, 32);
			this.btnsearch.TabIndex = 6;
			this.btnsearch.Text = "查询";
			this.btnsearch.Click += new System.EventHandler(this.btnsearch_Click);
			// 
			// cmbsearch
			// 
			this.cmbsearch.Items.AddRange(new object[] {
														   "姓名",
														   "电话号码",
														   "手机号码",
														   "qq号码",
														   "电子邮件"});
			this.cmbsearch.Location = new System.Drawing.Point(98, 26);
			this.cmbsearch.Name = "cmbsearch";
			this.cmbsearch.Size = new System.Drawing.Size(128, 22);
			this.cmbsearch.TabIndex = 5;
			this.cmbsearch.Text = "姓名";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label1.ForeColor = System.Drawing.Color.Black;
			this.label1.Location = new System.Drawing.Point(8, 29);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 23);
			this.label1.TabIndex = 4;
			this.label1.Text = "查询条件:";
			// 
			// btnexit
			// 
			this.btnexit.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnexit.Location = new System.Drawing.Point(640, 440);
			this.btnexit.Name = "btnexit";
			this.btnexit.Size = new System.Drawing.Size(88, 32);
			this.btnexit.TabIndex = 15;
			this.btnexit.Text = "退出";
			this.btnexit.Click += new System.EventHandler(this.btnexit_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.CaptionFont = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.CaptionText = "                                查询结果如下";
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 77);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(776, 339);
			this.dataGrid1.TabIndex = 18;
			// 
			// frmsearch
			// 
			this.AcceptButton = this.btnsearch;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(800, 485);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnexit);
			this.Name = "frmsearch";
			this.Text = "查询";
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		

		private void btnsearch_Click(object sender, System.EventArgs e)
		{
			string amountlike;//确定查询条件是等于还是类似
			string fields;//纪录查询字段
			string findvalue;//纪录查询字段的值;
			if (cmbsearch.Text=="姓名")
			{
				fields="name";
			}
			else if(cmbsearch.SelectedText=="电话号码")
			{
				fields="telephone";
			}
			else if(cmbsearch.SelectedText=="手机号码")
			{
				fields="mobilephone";
			}
			else if(cmbsearch.SelectedText=="QQ号码")
			{
				fields="qq";

			}
			else if(cmbsearch.SelectedText=="电子邮件")
			{
				fields="email";
			}
			else
			{
				MessageBox.Show("请从下拉列表中选择查询字段!");
				return;
			}

			if (txtsearchvalue.Text=="")
			{
				MessageBox.Show("请输入查询值");
				return;
			}

			if (cmbsearch2.Text=="等于") 
			{
				amountlike="=";
				findvalue="'" + txtsearchvalue.Text;
				findvalue += "'";
			}
			else
			{
				amountlike="like";
				findvalue="'%" + txtsearchvalue.Text;
				findvalue += "%'";
			}
			string strsearch="select name as '姓名',mobilepho as '移动电话',telephone as '固定电话',email as '电子邮件', ";
			strsearch += "qq as QQ,nowaddress as '现居地',familyaddress as '故乡',others as '备注',adddate as '添加日期',images as '照片' from stuinf where ";
			strsearch +=fields;
			strsearch +=" ";
			strsearch +=amountlike;
			strsearch +=" ";
			strsearch +=findvalue;
			Classdb searchdb=new Classdb();
			searchcon=searchdb.conds();
			SqlDataAdapter da=new SqlDataAdapter(strsearch,searchcon);
			//   da.SelectCommand.CommandText=strsearch;
			//da.SelectCommand.Connection=searchcon;
			//		int i;
			try
			{
				searchcon.Open();
				da.Fill(ds);
				count=ds.Tables[0].Rows.Count;
				if (count==0)
				{
					MessageBox.Show("没有找到相关纪录,请重新输入查询条件进行查询!");
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			finally
			{
				dataGrid1.DataSource= ds.Tables[0].DefaultView;
				searchcon.Close();
				da.Dispose();
			}
		}

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

⌨️ 快捷键说明

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