本周学习了怎样在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真正进行大作业的设计过程,不断修改,希望可以达到更加完善。