網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了C#實(shí)現(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ù)需要移動(dòng)或刪除它。 ? ? ? ? ?// ? 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里面的第一個(gè)參數(shù)是要添加的參數(shù)名,第二個(gè)參數(shù)是參數(shù)的數(shù)據(jù)類型,第三個(gè)是長度 ,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"); ? ? ? ? ? ? //下面的三個(gè)是賦值 ? ? ? ? ? ? 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); } ? ? ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/qq_26925297/article/details/80645808
相關(guān)推薦
- 2022-04-04 uni-app 數(shù)據(jù)上拉加載更多功能
- 2023-04-29 springboot整合單機(jī)緩存ehcache的實(shí)現(xiàn)_python
- 2022-02-07 使用laravel框架開發(fā)接口時(shí)post請求報(bào)錯(cuò)419,get請求正常
- 2023-04-02 python導(dǎo)出requirements.txt的幾種方法總結(jié)_python
- 2022-07-02 python中如何設(shè)置list步長_python
- 2022-11-03 Python?pandas中apply函數(shù)簡介以及用法詳解_python
- 2022-05-29 Docker容器下運(yùn)行Nginx并實(shí)現(xiàn)反向代理_docker
- 2022-09-10 Python入門之模塊和包用法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支