溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#如何實現(xiàn)學(xué)生檔案查詢

發(fā)布時間:2022-01-19 10:28:26 來源:億速云 閱讀:152 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)C#如何實現(xiàn)學(xué)生檔案查詢,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體內(nèi)容如下

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 參數(shù)查詢
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SqlDataAdapter sqlDataAdapter;
        private DataSet dataSet;
        private void Form1_Load(object sender, EventArgs e)
        {
 
            // TODO:  這行代碼將數(shù)據(jù)加載到表“xsglDataSet.student”中。您可以根據(jù)需要移動或刪除它。
         //   this.studentTableAdapter.Fill(this.xsglDataSet.student);
            //Sqlconnection就是建立到sqlserver數(shù)據(jù)庫的打開的連接
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = "server=localhost;uid=sa;pwd=root;database=xsgl";
            // SqlCommand對象用來對SQL Server數(shù)據(jù)庫執(zhí)行操作命令。
            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()添加參數(shù)到參數(shù)集,add里面的第一個參數(shù)是要添加的參數(shù)名,第二個參數(shù)是參數(shù)的數(shù)據(jù)類型,第三個是長度 ,Parameters的作用就是把存儲過程執(zhí)行結(jié)束后得到的參數(shù)傳到程序里
 
            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");
          dataGridView1.DataSource = dataSet;
            dataGridView1.DataMember = "student";
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try {
                if (textBox1.Text == "")
                {//如果沒有輸入id
                    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); }
 
 
        }
    }
}

C#如何實現(xiàn)學(xué)生檔案查詢

關(guān)于“C#如何實現(xiàn)學(xué)生檔案查詢”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI