C#實現(xiàn)學(xué)生模塊的增刪改查
更新時間:2022年01月24日 16:10:58 作者:rikiemo
這篇文章主要為大家詳細介紹了C#實現(xiàn)學(xué)生模塊的增刪改查,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#實現(xiàn)學(xué)生模塊的增刪改查的具體代碼,供大家參考,具體內(nèi)容如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace text3_CRUD { ? ? public partial class Form1 : Form ? ? { ? ? ? ? //把連接數(shù)據(jù)庫的字符串提取出來,就不用每次都要寫,增加代碼復(fù)用性 ? ? ? ? private string str = "data source=本地IP;initial catalog=數(shù)據(jù)庫名;user ID=用戶名;pwd=密碼"; ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? } ? ? ? ? ? ? ? ? private void TextBox5_TextChanged(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? ? ? private void Label10_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 添加學(xué)生信息檔案 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void ButAdd_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //獲取各個文本框的數(shù)據(jù) ? ? ? ? ? ? string name = txtname.Text; ? ? ? ? ? ? string sex = txtsex.Text; ? ? ? ? ? ? string college = txtcollege.Text; ? ? ? ? ? ? string id = txtid.Text; ? ? ? ? ? ? string grade = txtgrade.Text; ? ? ? ? ? ? string phone = txtphone.Text; ? ? ? ? ? ? string email = txtemail.Text; ? ? ? ? ? ? string qq = txtqq.Text; ? ? ? ? ? ? string room = txtroom.Text; ? ? ? ? ? ? using (var conn = new SqlConnection(this.str))//定義一個數(shù)據(jù)庫連接實例 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? conn.Open();//打開數(shù)據(jù)庫 ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? Form1 f = new Form1();//實例化Form1窗體對象 ? ? ? ? ? ? ? ? if (f.Existence(id, conn))//檢查數(shù)據(jù)庫 存不存在此條記錄,存在則插入 ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? SqlParameter[] para = new SqlParameter[]//構(gòu)建存儲過程的輸入?yún)?shù) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@name",name), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@sex", sex), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@college", college), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@id", id), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@grade", grade), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@phone", phone), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@email", email), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@qq", qq), ? ? ? ? ? ? ? ? ? ? ? ? new SqlParameter("@room", room), ? ? ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? ? ? ? ? string sql = "insert into Students values(@name, @sex, @college, @id, @grade, @phone, @email, @qq, @room);";//定義一個數(shù)據(jù)庫操作指令集 ? ? ? ? ? ? ? ? ? ? SqlCommand com = new SqlCommand(sql, conn);//執(zhí)行數(shù)據(jù)庫操作指令 ? ? ? ? ? ? ? ? ? ? com.Parameters.AddRange(para);//將參數(shù)和命令對象的參數(shù)集合綁定 ? ? ? ? ? ? ? ? ? ? int result = (int)com.ExecuteNonQuery();//針對Connection執(zhí)行的SQL語句,返回受影響的行數(shù),result > 0則表示SQL語句執(zhí)行成功 ? ? ? ? ? ? ? ? ? ? if (result > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("添加成功!");//彈窗顯示“添加成功”? ? ? ? ? ? ? ? ? ? ? ? ? this.Form1_Load_1(null, null);//刷新數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("添加失敗!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("數(shù)據(jù)已經(jīng)存在!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? conn.Close();//關(guān)閉數(shù)據(jù)庫 ? ? ? ? ? ? ? ? //Application.Exit();//關(guān)閉整個應(yīng)用程序 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 根據(jù)ID值判斷數(shù)據(jù)表Students中是否存在這個人,存在返回false,不存在返回true ? ? ? ? /// </summary> ? ? ? ? /// <param name="id"></param> ? ? ? ? /// <param name="conn"></param> ? ? ? ? /// <returns></returns> ? ? ? ? public bool Existence(string id, SqlConnection conn) ? ? ? ? { ? ? ? ? ? ? string txtStr = string.Format( "select id from Students where id = '{0}' " ,id);//定義一個數(shù)據(jù)庫操作指令集 ? ? ? ? ? ? SqlDataAdapter sda = new SqlDataAdapter(txtStr, conn);//定義一個數(shù)據(jù)庫適配器 ? ? ? ? ? ? DataSet ds = new DataSet();//定義數(shù)據(jù)集合 ? ? ? ? ? ? sda.Fill(ds);//填充數(shù)據(jù)集合 ? ? ? ? ? ? DataTable dt = ds.Tables[0];//將數(shù)據(jù)集合中的第一張表賦值給DataTable ? ? ? ? ? ? if(dt.Rows.Count > 0) ? //count > 0表示有數(shù)據(jù) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? ? ?? ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 對數(shù)據(jù)庫進行的動態(tài)查詢,不管用戶掌握的信息有多少都可以查詢 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void BtnSelect_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //獲取各個文本框的數(shù)據(jù) ? ? ? ? ? ? string name = txtname.Text; ? ? ? ? ? ? string sex = txtsex.Text; ? ? ? ? ? ? string college = txtcollege.Text; ? ? ? ? ? ? string id = txtid.Text; ? ? ? ? ? ? string grade = txtgrade.Text; ? ? ? ? ? ? string phone = txtphone.Text; ? ? ? ? ? ? string email = txtemail.Text; ? ? ? ? ? ? string qq = txtqq.Text; ? ? ? ? ? ? string room = txtroom.Text; ? ? ? ? ? ? using(var conn = new SqlConnection(this.str))//定義一個數(shù)據(jù)庫連接實例 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? conn.Open();//打開數(shù)據(jù)庫 ? ? ? ? ? ? ? ? StringBuilder sb = new StringBuilder();//創(chuàng)建一個字符串變量 ? ? ? ? ? ? ? ? sb.Append("select name, sex, college, id, grade,phone, email, qq, room from Students where 1=1"); ? ? ? ? ? ? ? ? //判斷用戶有沒有給出其它的查詢條件,有則添加進sql語句 ? ? ? ? ? ? ? ? if (name != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and name = '{0}'", name); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (sex != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and sex = '{0}'", sex); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (college != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and college = '{0}'", college); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (id != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and id = '{0}'", id); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (grade != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and grade = '{0}'", grade); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (phone != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and phone = '{0}'", phone); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (email != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and email = '{0}'", email); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (qq != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and qq = '{0}'", qq); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (room != "") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sb.AppendFormat(" and room = '{0}'", room); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? string sql = sb.ToString(); ? ? ? ? ? ? ? ? SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); ? ? ? ? ? ? ? ? DataSet ds = new DataSet();//定義一個數(shù)據(jù)集合 ? ? ? ? ? ? ? ? adapter.Fill(ds);//填充數(shù)據(jù)集合 ? ? ? ? ? ? ? ? dataGridView1.DataSource = ds.Tables[0];//把數(shù)據(jù)集合綁定到dataGridView上,dataGridView會以表格的形式顯示出來 ? ? ? ? ? ? ? ? conn.Close();//關(guān)閉數(shù)據(jù)庫 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 修改學(xué)生信息 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void BtnUpdate_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //獲取各個文本框的數(shù)據(jù) ? ? ? ? ? ? string name = txtname.Text; ? ? ? ? ? ? string sex = txtsex.Text; ? ? ? ? ? ? string college = txtcollege.Text; ? ? ? ? ? ? string id = txtid.Text; ? ? ? ? ? ? string grade = txtgrade.Text; ? ? ? ? ? ? string phone = txtphone.Text; ? ? ? ? ? ? string email = txtemail.Text; ? ? ? ? ? ? string qq = txtqq.Text; ? ? ? ? ? ? string room = txtroom.Text; ? ? ? ? ? ? //構(gòu)建存儲過程的輸入?yún)?shù) ? ? ? ? ? ? SqlParameter[] para = new SqlParameter[] ? ? ? ? ? ? { ? ? ? ? ? ? ? ? new SqlParameter("@name",name), ? ? ? ? ? ? ? ? new SqlParameter("@sex", sex), ? ? ? ? ? ? ? ? new SqlParameter("@college", college), ? ? ? ? ? ? ? ? new SqlParameter("@id", id), ? ? ? ? ? ? ? ? new SqlParameter("@grade", grade), ? ? ? ? ? ? ? ? new SqlParameter("@phone", phone), ? ? ? ? ? ? ? ? new SqlParameter("@email", email), ? ? ? ? ? ? ? ? new SqlParameter("@qq", qq), ? ? ? ? ? ? ? ? new SqlParameter("@room", room) ? ? ? ? ? ? }; ? ? ? ? ? ? using(var conn = new SqlConnection(this.str)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? conn.Open();//打開數(shù)據(jù)庫; ? ? ? ? ? ? ? ? string sql = "update Students set name = @name, sex = @sex, college = @college, id = @id, grade = @grade, phone = @phone, email = @email, qq = @qq, room = @room where id = @id"; ? ? ? ? ? ? ? ? SqlCommand com = new SqlCommand(sql, conn);//執(zhí)行數(shù)據(jù)庫操作指令 ? ? ? ? ? ? ? ? com.Parameters.AddRange(para);//將參數(shù)和命令對象的參數(shù)集合綁定 ? ? ? ? ? ? ? ? int result = (int)com.ExecuteNonQuery();//查詢返回的第一行第一列 ? ? ? ? ? ? ? ? if(result > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("修改成功!"); ? ? ? ? ? ? ? ? ? ? this.Form1_Load_1(null, null);//修改完數(shù)據(jù)后,重新刷新屬性Form1窗口,以查看變化的內(nèi)容 ? ? ? ? ? ? ? ? ? ? conn.Close();//關(guān)閉數(shù)據(jù)庫 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? //SqlDataAdapter sda = new SqlDataAdapter(); ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 刷新DataGridView里的數(shù)據(jù) ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void Form1_Load_1(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ?? ? ? ? ? ? ? using (var conn = new SqlConnection(this.str))//定義一個數(shù)據(jù)庫連接實例 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? conn.Open();//打開數(shù)據(jù)庫 ? ? ? ? ? ? ? ? string sql = "select * from Students";//定義一個數(shù)據(jù)庫操作指令 ? ? ? ? ? ? ? ? SqlDataAdapter sda = new SqlDataAdapter(sql, conn);//定義數(shù)據(jù)庫適配器 ? ? ? ? ? ? ? ? DataSet ds = new DataSet();//定義數(shù)據(jù)集合 ? ? ? ? ? ? ? ? sda.Fill(ds);//填充數(shù)據(jù)集合 ? ? ? ? ? ? ? ? dataGridView1.DataSource = ds.Tables[0]; ? ? ? ? ? ? ? ? conn.Close();//關(guān)閉數(shù)據(jù)庫 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) ? ? ? ? { ? ? ? ? ? ?? ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 選中DataGridView的行,這一行的數(shù)據(jù)返回到對應(yīng)的文本框 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void DataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e) ? ? ? ? { ?? ? ? ? ? ? ? //SelectedRows[0] 獲取用戶選定行的集合(選中的第一行就是0,一次類推) ? ? ? ? ? ? //Cells["name"] 獲取用于填充行的單元格集合(就是列) .Value就是它的值,最后ToString轉(zhuǎn)字符串 ? ? ? ? ? ? txtname.Text = dataGridView1.SelectedRows[0].Cells["name"].Value.ToString(); ? ? ? ? ? ? txtsex.Text = dataGridView1.SelectedRows[0].Cells["sex"].Value.ToString(); ? ? ? ? ? ? txtcollege.Text = dataGridView1.SelectedRows[0].Cells["college"].Value.ToString(); ? ? ? ? ? ? txtid.Text = dataGridView1.SelectedRows[0].Cells["id"].Value.ToString(); ? ? ? ? ? ? txtgrade.Text = dataGridView1.SelectedRows[0].Cells["grade"].Value.ToString(); ? ? ? ? ? ? txtphone.Text = dataGridView1.SelectedRows[0].Cells["phone"].Value.ToString(); ? ? ? ? ? ? txtemail.Text = dataGridView1.SelectedRows[0].Cells["email"].Value.ToString(); ? ? ? ? ? ? txtqq.Text = dataGridView1.SelectedRows[0].Cells["qq"].Value.ToString(); ? ? ? ? ? ? txtroom.Text = dataGridView1.SelectedRows[0].Cells["room"].Value.ToString(); ? ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 刪除某個學(xué)生的所有數(shù)據(jù) ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void BtnDelete_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? using(var conn = new SqlConnection(this.str))//創(chuàng)建一個數(shù)據(jù)庫連接實例 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? conn.Open();//連接數(shù)據(jù)庫 ? ? ? ? ? ? ? ? string sql = string.Format("delete from Students where id = '{0}'", txtid.Text);//往數(shù)據(jù)庫操作指令中傳值 ? ? ? ? ? ? ? ? //如果傳的值不是很多的話,就用這種方法;如果有很多就用SqlParameter[] ? ? ? ? ? ? ? ? SqlCommand com = new SqlCommand(sql, conn);//執(zhí)行數(shù)據(jù)庫刪除指令 ? ? ? ? ? ? ? ? int result = (int)com.ExecuteNonQuery();//返回結(jié)果,result > 0則為修改成功 ? ? ? ? ? ? ? ? if(result > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("刪除成功!"); ? ? ? ? ? ? ? ? ? ? this.Form1_Load_1(null, null);//刷新數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? conn.Close();//關(guān)閉數(shù)據(jù)庫 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 對文本框進行清空處理,方便重新輸入下一個學(xué)生信息 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void BtnClear_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? txtname.Text = null; ? ? ? ? ? ? txtsex.Text = null; ? ? ? ? ? ? txtcollege.Text = null; ? ? ? ? ? ? txtid.Text = null; ? ? ? ? ? ? txtgrade.Text = null; ? ? ? ? ? ? txtphone.Text = null; ? ? ? ? ? ? txtemail.Text = null; ? ? ? ? ? ? txtqq.Text = null; ? ? ? ? ? ? txtroom.Text = null; ? ? ? ? } ? ? } }
Students表
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# WinForm應(yīng)用程序降低系統(tǒng)內(nèi)存占用方法總結(jié)
這篇文章主要介紹了C# WinForm應(yīng)用程序降低系統(tǒng)內(nèi)存占用方法總結(jié),本文總結(jié)了9個方法,同時給出了一個定期清理執(zhí)行垃圾回收代碼,需要的朋友可以參考下2014-10-10C#調(diào)用C類型dll入?yún)閟truct的問題詳解
這篇文章主要給大家介紹了關(guān)于C#調(diào)用C類型dll入?yún)閟truct問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03c# Rank屬性與GetUpperBound方法的深入分析
本篇文章是對c#中的Rank屬性與GetUpperBound方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06