C#实现学生档案查询

本文实例为大家分享了C#实现学生档案查询的具体代码,供大家参考,具体内容如下

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace 参数查询{public partial class Form1 : Form{public Form1(){InitializeComponent(); }private SqlDataAdapter sqlDataAdapter; private DataSet dataSet; private void Form1_Load(object sender, EventArgs e){ // TODO:这行代码将数据加载到表“xsglDataSet.student”中。您可以根据需要移动或删除它。//this.studentTableAdapter.Fill(this.xsglDataSet.student); //Sqlconnection就是建立到sqlserver数据库的打开的连接SqlConnection myConnection = new SqlConnection(); myConnection.ConnectionString = "server=localhost; uid=sa; pwd=root; database=xsgl"; // SqlCommand对象用来对SQL Server数据库执行操作命令。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"; //comm.Parameters.Add()添加参数到参数集,add里面的第一个参数是要添加的参数名,第二个参数是参数的数据类型,第三个是长度 ,Parameters的作用就是把存储过程执行结束后得到的参数传到程序里 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 = "https://www.it610.com/article/%"; sqlCommand.Parameters["@studName"].Value = "https://www.it610.com/article/%"; sqlCommand.Parameters["@studSex"].Value = "https://www.it610.com/article/%"; sqlDataAdapter = new SqlDataAdapter(); dataSet = new DataSet(); sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataSet,"student"); dataGridView1.DataSource = dataSet; dataGridView1.DataMember = "student"; } private void button1_Click(object sender, EventArgs e){try {if (textBox1.Text == ""){//如果没有输入idsqlDataAdapter.SelectCommand.Parameters["@studID"].Value = "https://www.it610.com/article/%"; }else {sqlDataAdapter.SelectCommand.Parameters["@studID"].Value = https://www.it610.com/article/textBox1.Text; } if (textBox2.Text ==""){//如果没有输入姓名sqlDataAdapter.SelectCommand.Parameters["@studName"].Value = "https://www.it610.com/article/%"; }else{sqlDataAdapter.SelectCommand.Parameters["@studName"].Value = https://www.it610.com/article/textBox2.Text; } //if (comboBox1.SelectedIndex == 0) { sqlDataAdapter.SelectCommand.Parameters["@studSex"].Value = "https://www.it610.com/article/%"; }else if (comboBox1.SelectedIndex == 1){sqlDataAdapter.SelectCommand.Parameters["@studSex"].Value = "https://www.it610.com/article/男"; }else {sqlDataAdapter.SelectCommand.Parameters["@studSex"].Value = "https://www.it610.com/article/女"; } dataSet.Tables["student"].Clear(); sqlDataAdapter.Fill(dataSet,"student"); }catch (SqlException ee) { MessageBox.Show(ee.Message); } }}}

C#实现学生档案查询
文章图片

【C#实现学生档案查询】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读