在前两个实验作业中已经知道了在VS中如何建立新的项目和设计页面,所以也不在此多做介绍了,这个页面的设计。
先解释一下,因为刚设计的时候,忘了写取消的按钮,因此下面的截图中取消键不会出现,还有就是截图的顺序不是按照增删改查依次执行的顺序。
增加
private void button1_Click(object sender, EventArgs e) //增加
{
String StuID = textBoxSno.Text.Trim();
String StuName = textBoxName.Text.Trim();
String StuSex = textBoxSex.Text.Trim();
String StuDept = textBoxDept.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456");
try
{
con.Open();
string insertStr = "INSERT INTO STU(Sno,Sname,Ssex,Sdept) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuDept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
}
在其中一定要注意的是对于SQL语句中的引用赋值中,若是用的字符类型,要加单引号,再次着重强调。
string insertStr = "INSERT INTO STU(Sno,Sname,Ssex,Sdept) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuDept + "')";
private void button2_Click(object sender, EventArgs e) //删除
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//选择的当前行第一列的值,也就是Sno
string delete_by_id = "delete from STU where Sno=" + select_id;
//删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请选择正确的行!");
}
finally
{
con.Dispose();
}
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
}
这部分中,SQL语句里,**delete from STU where Sno=**中的=不要忘记
(参考上图)
修改
private void button3_Click(object sender, EventArgs e) //修改
{
String StuID = textBoxSno.Text.Trim();
String StuName = textBoxName.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456");
try
{
con.Open();
string insertStr = "UPDATE STU SET Sname='" + StuName + "' WHERE Sno ='" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
}
private void button4_Click(object sender, EventArgs e) //查找
{
String StuID = textBoxSno.Text.Trim();
String conn = "Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
String select_by_id = "select * from STU where Sno='"+ StuID +"'";
SqlCommand cmd = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = cmd.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
sqlConnection.Dispose();
}
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
全部代码
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 TestCRUD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“pRIMARYDataSet2.STU”中。您可以根据需要移动或删除它。
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
// TODO: 这行代码将数据加载到表“pRIMARYDataSet1.STU”中。您可以根据需要移动或删除它。
}
private void button1_Click(object sender, EventArgs e) //增加
{
String StuID = textBoxSno.Text.Trim();
String StuName = textBoxName.Text.Trim();
String StuSex = textBoxSex.Text.Trim();
String StuDept = textBoxDept.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456");
try
{
con.Open();
string insertStr = "INSERT INTO STU(Sno,Sname,Ssex,Sdept) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuDept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //删除
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//选择的当前行第一列的值,也就是Sno
string delete_by_id = "delete from STU where Sno=" + select_id;
//删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请选择正确的行!");
}
finally
{
con.Dispose();
}
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
}
private void button3_Click(object sender, EventArgs e) //修改
{
String StuID = textBoxSno.Text.Trim();
String StuName = textBoxName.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456");
try
{
con.Open();
string insertStr = "UPDATE STU SET Sname='" + StuName + "' WHERE Sno ='" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sTUTableAdapter.Fill(this.pRIMARYDataSet2.STU);
}
private void button4_Click(object sender, EventArgs e) //查找
{
String StuID = textBoxSno.Text.Trim();
String conn = "Data Source=.;Initial Catalog=PRIMARY;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
String select_by_id = "select * from STU where Sno='"+ StuID +"'";
SqlCommand cmd = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = cmd.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
sqlConnection.Dispose();
}
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
作业写的比较晚,也出现了一点小小的问题,在SQL语句的查找语句中我总是忘了写=,以至于最后的运行结果有点问题,给我自己长一点记性。