C#實現學生模塊的增刪改查
本文實例為大傢分享瞭C#實現學生模塊的增刪改查的具體代碼,供大傢參考,具體內容如下
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 { //把連接數據庫的字符串提取出來,就不用每次都要寫,增加代碼復用性 private string str = "data source=本地IP;initial catalog=數據庫名;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> /// 添加學生信息檔案 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButAdd_Click(object sender, EventArgs e) { //獲取各個文本框的數據 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))//定義一個數據庫連接實例 { conn.Open();//打開數據庫 Form1 f = new Form1();//實例化Form1窗體對象 if (f.Existence(id, conn))//檢查數據庫 存不存在此條記錄,存在則插入 { 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), }; string sql = "insert into Students values(@name, @sex, @college, @id, @grade, @phone, @email, @qq, @room);";//定義一個數據庫操作指令集 SqlCommand com = new SqlCommand(sql, conn);//執行數據庫操作指令 com.Parameters.AddRange(para);//將參數和命令對象的參數集合綁定 int result = (int)com.ExecuteNonQuery();//針對Connection執行的SQL語句,返回受影響的行數,result > 0則表示SQL語句執行成功 if (result > 0) { MessageBox.Show("添加成功!");//彈窗顯示“添加成功” this.Form1_Load_1(null, null);//刷新數據 } else { MessageBox.Show("添加失敗!"); } } else { MessageBox.Show("數據已經存在!"); } conn.Close();//關閉數據庫 //Application.Exit();//關閉整個應用程序 } } /// <summary> /// 根據ID值判斷數據表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);//定義一個數據庫操作指令集 SqlDataAdapter sda = new SqlDataAdapter(txtStr, conn);//定義一個數據庫適配器 DataSet ds = new DataSet();//定義數據集合 sda.Fill(ds);//填充數據集合 DataTable dt = ds.Tables[0];//將數據集合中的第一張表賦值給DataTable if(dt.Rows.Count > 0) //count > 0表示有數據 { return false; } else { return true; } } /// <summary> /// 對數據庫進行的動態查詢,不管用戶掌握的信息有多少都可以查詢 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSelect_Click(object sender, EventArgs e) { //獲取各個文本框的數據 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))//定義一個數據庫連接實例 { conn.Open();//打開數據庫 StringBuilder sb = new StringBuilder();//創建一個字符串變量 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();//定義一個數據集合 adapter.Fill(ds);//填充數據集合 dataGridView1.DataSource = ds.Tables[0];//把數據集合綁定到dataGridView上,dataGridView會以表格的形式顯示出來 conn.Close();//關閉數據庫 } } /// <summary> /// 修改學生信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnUpdate_Click(object sender, EventArgs e) { //獲取各個文本框的數據 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; //構建存儲過程的輸入參數 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();//打開數據庫; 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);//執行數據庫操作指令 com.Parameters.AddRange(para);//將參數和命令對象的參數集合綁定 int result = (int)com.ExecuteNonQuery();//查詢返回的第一行第一列 if(result > 0) { MessageBox.Show("修改成功!"); this.Form1_Load_1(null, null);//修改完數據後,重新刷新屬性Form1窗口,以查看變化的內容 conn.Close();//關閉數據庫 } } //SqlDataAdapter sda = new SqlDataAdapter(); } /// <summary> /// 刷新DataGridView裡的數據 /// </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))//定義一個數據庫連接實例 { conn.Open();//打開數據庫 string sql = "select * from Students";//定義一個數據庫操作指令 SqlDataAdapter sda = new SqlDataAdapter(sql, conn);//定義數據庫適配器 DataSet ds = new DataSet();//定義數據集合 sda.Fill(ds);//填充數據集合 dataGridView1.DataSource = ds.Tables[0]; conn.Close();//關閉數據庫 } } private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { } /// <summary> /// 選中DataGridView的行,這一行的數據返回到對應的文本框 /// </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轉字符串 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> /// 刪除某個學生的所有數據 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDelete_Click(object sender, EventArgs e) { using(var conn = new SqlConnection(this.str))//創建一個數據庫連接實例 { conn.Open();//連接數據庫 string sql = string.Format("delete from Students where id = '{0}'", txtid.Text);//往數據庫操作指令中傳值 //如果傳的值不是很多的話,就用這種方法;如果有很多就用SqlParameter[] SqlCommand com = new SqlCommand(sql, conn);//執行數據庫刪除指令 int result = (int)com.ExecuteNonQuery();//返回結果,result > 0則為修改成功 if(result > 0) { MessageBox.Show("刪除成功!"); this.Form1_Load_1(null, null);//刷新數據 conn.Close();//關閉數據庫 } } } /// <summary> /// 對文本框進行清空處理,方便重新輸入下一個學生信息 /// </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表
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- C#數據表格(DataGridView)控件的應用案例
- c# 使用線程對串口serialPort進行收發數據(四種)
- 詳解DataGridView控件的數據綁定
- c# Winform同一數據源多個控件保持同步
- C#開發WinForm根據條件改變DataGridView行顏色