实验三:增、删、改、查

本实验涉及数据库操作,包括使用dataGridView添加数据源,实现Student数据的导入。详细步骤涵盖增加记录、选择记录进行删除、根据学号修改姓名以及执行特定学号的查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一个实验中利用dataGridView添加数据源,将Student中的数据导入进来,并添加好需要的控件。
在这里插入图片描述

  1. 增加
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023");
        private void buttonInsert_Click(object sender, EventArgs e)//增加
        {
            string StuSno = textBox1.Text.Trim();
            string StuSname = textBox2.Text.Trim();
            string StuSsex = textBox3.Text.Trim();
            string StuSage = textBox4.Text.Trim();
            string StuSdept = textBox4.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.textDataSet.Student);

        }

在这里插入图片描述

2.删除

 private void button2_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);

        }

选中
在这里插入图片描述
在这里插入图片描述
3.修改

 private void button3_Click(object sender, EventArgs e)//修改
        {
            string StuSno = textBox1.Text.Trim();
            string StuSname = textBox2.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.textDataSet.Student);

        }

根据学号改姓名
在这里插入图片描述
4.查询

private void button4_Click(object sender, EventArgs e)
        {
            string StuSno = textBox1.Text.Trim();
            String conn = "Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023";
            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;
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

查询201215121
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值