📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Exam6_3_1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.comboBox1);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(1, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(399, 48);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "查询条件";
//
// button1
//
this.button1.Location = new System.Drawing.Point(312, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 8;
this.button1.Text = "查询";
this.button1.Click += new System.EventHandler(this.query);
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"所有",
"男",
"女"});
this.comboBox1.Location = new System.Drawing.Point(252, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(48, 20);
this.comboBox1.TabIndex = 5;
this.comboBox1.Text = "所有";
//
// label3
//
this.label3.Location = new System.Drawing.Point(217, 20);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(32, 24);
this.label3.TabIndex = 4;
this.label3.Text = "性别";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(131, 16);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(72, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(98, 20);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 24);
this.label2.TabIndex = 2;
this.label2.Text = "姓名";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 15);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(48, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(9, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 24);
this.label1.TabIndex = 0;
this.label1.Text = "学号";
//
// dataGrid1
//
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 64);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.RowHeadersVisible = false;
this.dataGrid1.Size = new System.Drawing.Size(400, 136);
this.dataGrid1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 205);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "学生档案查询";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private SqlDataAdapter sqlDataAdapter;
private DataSet dataSet;
private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "server=localhost;uid=sa;pwd=;database=xsgl";
SqlCommand sqlCommand=new SqlCommand();
sqlCommand.Connection=myConnection;;
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = "select * from student where studID like @studID and studName like @studName and studSex like @studSex";
sqlCommand.Parameters.Add("@studID",System.Data.SqlDbType.VarChar,10,"studID");
sqlCommand.Parameters.Add("@studName",System.Data.SqlDbType.VarChar,10,"studName");
sqlCommand.Parameters.Add("@studSex",System.Data.SqlDbType.VarChar,2,"studSex");
sqlCommand.Parameters["@studID"].Value="%";
sqlCommand.Parameters["@studName"].Value="%";
sqlCommand.Parameters["@studSex"].Value ="%";
sqlDataAdapter=new SqlDataAdapter();
dataSet=new DataSet();
sqlDataAdapter.SelectCommand=sqlCommand;
sqlDataAdapter.Fill(dataSet,"Student");
dataGrid1.DataSource=dataSet;
dataGrid1.DataMember="Student";
}
private void query(object sender, System.EventArgs e)
{
try
{
if(textBox1.Text=="")
sqlDataAdapter.SelectCommand.Parameters["@studID"].Value="%";
else
sqlDataAdapter.SelectCommand.Parameters["@studID"].Value = textBox1.Text;
if(textBox2.Text=="")
sqlDataAdapter.SelectCommand.Parameters["@studName"].Value="%";
else
sqlDataAdapter.SelectCommand.Parameters["@studName"].Value = textBox2.Text;
if(comboBox1.SelectedIndex==0)
sqlDataAdapter.SelectCommand.Parameters["@studSex"].Value ="%";
else if(comboBox1.SelectedIndex==1)
sqlDataAdapter.SelectCommand.Parameters["@studSex"].Value ="男";
else
sqlDataAdapter.SelectCommand.Parameters["@studSex"].Value ="女";
dataSet.Tables["Student"].Clear();
sqlDataAdapter.Fill(dataSet,"Student");
}
catch(SqlException ee){MessageBox.Show(ee.Message);}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -