新建程序,添加一个能显示数据库信息的DataGirdView,添加了要显示的数据源,具体操作上节课已经学习过了。
设计界面如下:
代码如下:
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 TestCURD
{
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);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e) //增加
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox5.Text.Trim();
String StuAge = textBox4.Text.Trim();
//数据库的连接定义
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;User ID=sa;Password=123456");
try
{
con.Open();//数据库打开
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuAge + "'," + StuSdept + ")";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();//执行语句
}
catch//try中有错误执行
{
MessageBox.Show("输入数据违反要求!");
}
finally//数据库释放
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.studentDataSet.Student);//刷新一遍
}
private void buttonDelete_Click(object sender, EventArgs e)//删除
{
//数据库连接,其实不需要每个都定义一遍,可以在外侧定义一遍
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;User ID=sa;Password=123456");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void buttonRevise_Click(object sender, EventArgs e)//修改
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuSdept = textBox5.Text.Trim();
String StuAge = textBox4.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;User ID=sa;Password=123456");
try
{
con.Open();
//根据学号进行修改
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
string insertStr2 = "UPDATE Student SET SSex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd2 = new SqlCommand(insertStr2, con);
cmd2.ExecuteNonQuery();
string insertStr3 = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd3 = new SqlCommand(insertStr3, con);
cmd3.ExecuteNonQuery();
string insertStr4 = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd4 = new SqlCommand(insertStr4, con);
cmd4.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void buttonSelect_Click(object sender, EventArgs e)//查找
{
String StuID = textBox1.Text.Trim();
String conn = "Data Source=.;Initial Catalog=Student;User ID=sa;Password=123456";
SqlConnection sqlConnection = new SqlConnection(conn); //实例化连接对象
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
sqlConnection.Close();
}
}
private void buttonClose_Click(object sender, EventArgs e)
{
Application.Exit();//关闭应用程序
}
}
}
在测试期间我发现,如果双击了一个控件,也就是会进入到编辑代码界面,会随之出现一个关于这个控件的空白函数,如果觉得没用想要删除掉这个空白函数的话,再次进入设计界面时就会报错,忽略错误的话设计界面也会变得面目全非,这时在代码界面把删除掉的空白函数再次加上就行了(可以按ctrl+z恢复),设计界面也会完好如初。就是点过这个控件那就不能删掉关于这个控件的函数。
检测程序时,可以在程序之中加入断点(点击程序行数左侧的小空白,出现一个红色的点),那么启动程序时在断点处会停止。点击继续按钮可以继续运行。
断点SqlCommand cmd = new SqlCommand(insertStr, con);输入学号等数据,点击Insert后将光标放到这一行代码上,点击小搜索图标,将值中的代码放到SQL server中运行,发现总是出现以下错误。
INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept) VALUES ('1','1','11','1',1)
--对象名 'StudentInsertLog' 无效。
仔细看也找不到错误,后来搜索到一点关于建立触发器后对象名会无效的情况,然后看到我的Student表真的建立过触发器。删掉触发器再执行就没有问题了。
中间在删除函数中try的第三行中加入了一个断点,因为我删除一行时总是提醒我“请选择正确行!”,我就想加个断点吧,我看看转换成SQL语句什么样子。结果加入断点后还是显示“请选择正确行!”,我想try里有个断点,而“请选择正确行”的代码在catch里边,怎么着也执行不到catch里去。看了好久,突然发现我密码写错了。改了密码,就可以了。。