本週學習了怎樣在DataGridView上面對數據進行增刪改查,增刪改的操作基本相同,查詢的操作略有不同,需要將查詢的值賦給數據源,之後數據源再賦給DataGridView.
注意:對於一些變量可以設置爲全局變量,如連接數據庫時的變量寫爲全局變量,這樣在修改連接的數據庫時會方便很多。
這裏的修改是根據學號修改姓名,可以根據實際需要進行其他操作;查詢是通過學號查詢;在增加信息時應該對年齡、性別的輸入作具體的要求,如年齡>0,性別只能爲“男”或“女”,在接下來的實驗中應該做具體的要求。
代碼:
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 CURD1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據加載到表“studentDataSet.Student”中。您可以根據需要移動或刪除它。
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Student; Persist Security Info = True;User ID = sa; Password = 123"); //連接數據庫
private void buttonadd_Click(object sender, EventArgs e)//增加
{
string StuSno = textBoxsno.Text.Trim();
string StuSname = textBoxsname.Text.Trim();
string StuSsex = textBoxssex.Text.Trim();
string StuSage = textBoxsage.Text.Trim();
string StuSdept = textBoxsdept.Text.Trim();
//SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Student; Persist Security Info = True;User ID = sa; Password = 123"); //連接數據庫
try
{
con.Open(); //打開數據庫
string insertStr = "INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept)" + "VALUES('" + StuSno + "','" + StuSname + "','" + StuSsex + "'," + StuSage + ",'" + StuSdept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery(); //將增加後的信息直接出來
}
catch
{
MessageBox.Show("輸入數據違法要求!");
}
finally
{
con.Dispose(); //關閉數據庫
}
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void buttondelete_Click(object sender, EventArgs e)//刪除
{
try
{
con.Open(); //打開數據庫
string select_Sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是Sno
string delete_by_Sno = "DELETE FROM Student WHERE Sno='" + select_Sno+"'";//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_Sno, con);
cmd.ExecuteNonQuery(); //將增加後的信息直接出來
}
catch
{
MessageBox.Show("請選擇正確行!");
}
finally
{
con.Dispose(); //關閉數據庫
}
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void buttonupdate_Click(object sender, EventArgs e)//修改,根據學號修改姓名
{
string StuSno = textBoxsno.Text.Trim();
string StuSname = textBoxsname.Text.Trim();
try
{
con.Open(); //打開數據庫
string update_sname = "UPDATE Student SET Sname='" + StuSname + "'WHERE Sno='" + StuSno+"'";
SqlCommand cmd = new SqlCommand(update_sname ,con);
cmd.ExecuteNonQuery(); //將增加後的信息直接出來
}
catch
{
MessageBox.Show("輸入數據違反要求");
}
finally
{
con.Dispose(); //關閉數據庫
}
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void buttonselect_Click(object sender, EventArgs e)//查找,根據學號查找
{
string StuSno = textBoxsno.Text.Trim();
String conn = "Data Source =.; Initial Catalog = Student; Persist Security Info = True;User ID = sa; Password = 123";
SqlConnection sqlconnection = new SqlConnection(conn);//實例化連接對象
try
{
sqlconnection.Open();
String select_by_sno = "select * from Student where Sno='" + StuSno + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_sno, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
//將讀出來的值賦給數據源,再將數據源給dataGridView
}
catch
{
MessageBox.Show("查詢語句有誤,請認真檢查SQL語句");
}
finally
{
sqlconnection.Close();
}
}
private void buttonclose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
數據測試:
(1)增加:
(2)刪除:刪除學號爲:201215128的陳冬
(3)修改:修改學號爲1的姓名爲李勇
(4)查詢:查詢學號爲201215122的學生信息
學習C#的基礎到此結束,我也學到了不少的新東西,下面開始使用C#+SQL SERVER真正進行大作業的設計過程,不斷修改,希望可以達到更加完善。