C#實(shí)現(xiàn)學(xué)生檔案查詢
本文實(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ù)庫(kù)的打開(kāi)的連接 ? ? ? ? ? ? SqlConnection myConnection = new SqlConnection(); ? ? ? ? ? ? myConnection.ConnectionString = "server=localhost;uid=sa;pwd=root;database=xsgl"; ? ? ? ? ? ? // SqlCommand對(duì)象用來(lái)對(duì)SQL Server數(shù)據(jù)庫(kù)執(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è)是長(zhǎng)度 ,Parameters的作用就是把存儲(chǔ)過(guò)程執(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 == "") ? ? ? ? ? ? ? ? {//如果沒(méi)有輸入id ? ? ? ? ? ? ? ? ? ? sqlDataAdapter.SelectCommand.Parameters["@studID"].Value = "%"; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? ? ? sqlDataAdapter.SelectCommand.Parameters["@studID"].Value = textBox1.Text; ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? if (textBox2.Text == "") ? ? ? ? ? ? ? ? {//如果沒(méi)有輸入姓名 ? ? ? ? ? ? ? ? ? ? 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); } ? ? ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#使用簡(jiǎn)單工廠模式實(shí)現(xiàn)生成html文件的封裝類分享
這篇文章主要介紹了運(yùn)用了簡(jiǎn)單工廠模式實(shí)現(xiàn)頁(yè)面靜態(tài)化封裝類,思路比較簡(jiǎn)單,大家可根據(jù)自己的思路再擴(kuò)展此類2014-01-01C#使用Dynamic實(shí)現(xiàn)簡(jiǎn)化反射
這篇文章主要為大家詳細(xì)介紹了C#如何使用Dynamic來(lái)實(shí)現(xiàn)簡(jiǎn)化反射,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07使用Visual Studio2019創(chuàng)建C#項(xiàng)目(窗體應(yīng)用程序、控制臺(tái)應(yīng)用程序、Web應(yīng)用程序)
這篇文章主要介紹了使用Visual Studio2019創(chuàng)建C#項(xiàng)目(窗體應(yīng)用程序、控制臺(tái)應(yīng)用程序、Web應(yīng)用程序),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-03-03基于c#用Socket做一個(gè)局域網(wǎng)聊天工具
目前基于Internet的即時(shí)聊天工具已經(jīng)做的非常完美,本文介紹了基于c#用Socket做一個(gè)局域網(wǎng)聊天工具,有需要的朋友可以看一下。2016-10-10C#實(shí)現(xiàn)CSV文件讀寫(xiě)的示例詳解
這篇文章主要介紹了CsvHelper、TextFieldParser、正則表達(dá)式三種解析CSV文件的方法,順帶也會(huì)介紹一下CSV文件的寫(xiě)方法,需要的可以參考一下2023-05-05