網站首頁 編程語言 正文
本文實例為大家分享了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數據庫執(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()添加參數到參數集,add里面的第一個參數是要添加的參數名,第二個參數是參數的數據類型,第三個是長度 ,Parameters的作用就是把存儲過程執(zhí)行結束后得到的參數傳到程序里 ? ? ? ? ? ? ? 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); } ? ? ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/qq_26925297/article/details/80645808
相關推薦
- 2022-06-04 .NET項目在k8s中運行的Dapr持續(xù)集成流程_ASP.NET
- 2022-08-04 GoFrame框架gcache的緩存控制淘汰策略實踐示例_Golang
- 2022-07-02 iview中的表格render搭配使用Tooltip 文字提示
- 2022-06-23 C語言實現鏈表與文件存取的示例代碼_C 語言
- 2022-04-17 jQuery的基本使用
- 2022-04-18 C#實現彈窗提示輸入密碼_C#教程
- 2023-03-01 shell?創(chuàng)建子進程及并行延時執(zhí)行命令方法_linux shell
- 2022-04-19 C#中IntPtr類型的具體使用_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支