【大作业进度3】VS增删改查C#

以下的增删改查是管理员界面的,在一个界面里就全部完成了。学生界面我准备用弹框方式做(就是选择查询,弹出来一个框那种),本来这个也应该做弹框的,但是都做完了我才想起来,所以我就没改,准备做学生界面的时候再弄(说白了还是懒)

依旧是先排版,这里就不多说了。

然后设置增删改查操作,具体见下边代码及注释。
检测增删改查语句正确与否,增加断点,运行。
在这里插入图片描述
在这里插入图片描述
在代码部分找到相关语句,鼠标移动过去就会出现语句
在这里插入图片描述
点这个小放大镜就会出现可视化工具
在这里插入图片描述
(ps:我在界面的文本框放反了,年龄的放到专业下边去了。会弹出框提示添加不成功)
改正后,添加成功⬇
在这里插入图片描述
删除⬇
在这里插入图片描述
修改⬇
修改信息时,只能修改第一次输入的学号,改为Close就可以了。
con.Dispose():释放内存里的con,并没有断开连接
con.Close():断开连接
我设置了文本框不为空时修改,可以一次修改多个属性。
但是出现了新BUG:不可以修改属性为空。没想出来咋解决,只能先输入0和null代替。
在这里插入图片描述
在这里插入图片描述
查询⬇
在基础上,增加了多条件查询的功能。设置flag变量代表单条件和多条件,多条件时sql语句的字符串上加And(代码在最后
在这里插入图片描述
在这里插入图片描述
返回⬇
点返回按钮回到初始操作界面。
代码:清空文本框内容并且查询显示全表
在这里插入图片描述
源代码:(只放了操作的代码,一开始那一堆乱七八糟的没放

//************************关闭***********************
        private void button5_Click(object sender, EventArgs e)      //关闭
        {
            this.Close();
        }
        
//*****************************删除*****************************
        private void button2_Click(object sender, EventArgs e)   
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou");     //定义
            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.sTUDENTDataSet1.Student);
        }
        
//***************************增添*************************
        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 = textBox4.Text.Trim();
            String StuAge = textBox5.Text.Trim();                 //读取需要插入的值
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou");
            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
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Dispose();
            }
            this.studentTableAdapter.Fill(this.sTUDENTDataSet1.Student);
        }
        
//*********************************修改*****************************
        private void button3_Click(object sender, EventArgs e)   
        {
            String StuID = textBox1.Text.Trim();
            String StuName = textBox2.Text.Trim();
            String StuSex = textBox3.Text.Trim();
            String StuSdept = textBox4.Text.Trim();
            String StuAge = textBox5.Text.Trim();
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou");
            try
            {
                string insertStr = "";
                string insertStr1="";
                string insertStr2 = "";
                string insertStr3 = "";
                con.Open();
                if (StuName != "")
                {
                    insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";    //修改名字
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }

                if (StuSex != "")
                {
                    insertStr1 = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";    //修改性别
                    SqlCommand cmd1 = new SqlCommand(insertStr1, con);
                    cmd1.ExecuteNonQuery();
                }
                if (StuSdept != "")
                {
                    insertStr2 = "UPDATE Student SET Sdept = '" + StuSdept+ "' WHERE Sno = '" + StuID + "'";    //修改专业
                    SqlCommand cmd2 = new SqlCommand(insertStr2, con);
                    cmd2.ExecuteNonQuery();
                }
                if (StuAge != "")
                {
                    insertStr3 = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";    //修改年龄
                    SqlCommand cmd3 = new SqlCommand(insertStr3, con);
                    cmd3.ExecuteNonQuery();
                }

            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Close();
            }
            this.studentTableAdapter.Fill(this.sTUDENTDataSet1.Student);
        }
        
//*****************************查找**************************
        private void button4_Click(object sender, EventArgs e)    
        {
            String StuID = textBox1.Text.Trim();
            String StuName = textBox2.Text.Trim();
            String StuSex = textBox3.Text.Trim();
            String StuSdept = textBox4.Text.Trim();
            String StuAge = textBox5.Text.Trim();
            String conn = "Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou";
            SqlConnection sqlConnection = new SqlConnection(conn);  //实例化连接对象
            try
            {
                String select_by_id = "select * from Student where ";
                int flag = 0;  //0是单条件,1是多条件
                sqlConnection.Open();
                if(StuID!="")
                select_by_id += "Sno='" + StuID + "'";    //单条件 按学号
                if (StuName != "")
                {
                    if (flag == 0) 
                    {
                        select_by_id += "Sname='" + StuName + "'";
                        flag = 1; 
                    }
                    if(flag==1)
                        select_by_id += "And Sname='" + StuName + "'";

                }
                if (StuAge != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Sage='" + StuAge + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Sage='" + StuAge + "'";
                }
                if(StuSdept!="")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Sdept='" + StuSdept + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Sdept='" + StuSdept + "'";
                }
                if(StuSex!="")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Ssex='" + StuSex + "'";
                        flag = 1;
                    }
                    if (flag == 1)
                        select_by_id += "And Ssex='" + StuSex + "'";
                }
                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 button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            String conn = "Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou";
            SqlConnection sqlConnection = new SqlConnection(conn);  //实例化连接对象
            try
            {
                String select_by_id = "select * from Student";
                
                sqlConnection.Open();  
            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();
            }        
        }
    }
}

因为老师课上已经讲了增删改查的简单代码,所以大的框架在,自己理解一下代码,再完善一下功能就可以了,测试了下,目前没有测出bug来,也可能是我没有测试完整。噢对了,性别限制只能输入男女我在数据库加了个限制,就一sql语句,写在下篇了。

原创文章 20 获赞 37 访问量 9236

猜你喜欢

转载自blog.csdn.net/qq_44871112/article/details/106109360
今日推荐