【数据库实验3】VS——C#增、删、改、查


 

  任务:增加增、删、改、查的功能

 

目录

增加组件、优化界面

通用代码

界面

 

代码

结果

结果

代码

结果

界面

代码

结果

取消

刷新

 

插眼:ComboBox使用

插眼:设置成只读

插眼:(未解决)两个FORM连接。————已经解决

 


 

增加组件、优化界面

 

 

 

通用代码

 

             SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");        //新建连接
            try
            {
                con.Open();        //打开
                string ComStr = ————        //SQL语句
                SqlCommand cmd = new SqlCommand(ComStr, con);
                cmd.ExecuteNonQuery();                            //返回执行命令后影响的参数
                this.Close();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }

 

  注:这里出现的错误,点一下就解决了。

 


 

 

界面

 

  新建一个form。

  因为性别、系别为固定选项,则选择ComboBox组件。

 

 

插眼:ComboBox使用

 

  属性中Items里,写入下拉框的内容,一行为一项。

 

 

 

 

插眼:设置成只读

 

  只读即为只显示,无法编辑。

  将ComboBox设置成只读。

  属性中,找到DropDownStyle,将其改为DropDownList。

 

 

 

代码

 

  用try-catch防止出现问题。

  新建一个SqlConnection类 con,连接SQL数据库。内容与之前的相同。

 

SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");

 

  添加语句:

 

string ComStr = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept)" + "VALUES ('" + StuNum + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuDept + "')";        //SQL添加语句

 

调用参数:

 

public Form5(string select_id)
{
    this.select_id = select_id;        //Sno主码
}

 

 

  代码:

 

        private void button1_Click(object sender, EventArgs e)
        {
            string StuNum = textBox1.Text.Trim();
            string StuName = textBox2.Text.Trim();
            string StuSex = comboBox1.Text;
            string StuAge = textBox3.Text.Trim();
            string StuDept = comboBox2.Text;        //提取几组数据

            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                con.Open();        //打开
                string ComStr = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept)" + "VALUES ('" + StuNum + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuDept + "')";        //SQL添加语句
                SqlCommand cmd = new SqlCommand(ComStr, con);
                cmd.ExecuteNonQuery();                            //返回执行命令后影响的参数
                MessageBox.Show("添加成功!请点击“刷新”按钮");
                this.Close();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }

        }

 

  主界面代码:

 

private void button2_Click(object sender, EventArgs e)          //增
{
    Form5 form5 = new Form5();
    form5.Show();
}

 

 

结果

 

  添加成功

 

 

  Form中的结果。

 

 

  SQL中的结果。

 

 


 

 

插眼:(未解决)两个FORM连接。————已经解决

 

  原本打算这四个功能都与“增”一样新建一个Form再操作,然后主界面的代码只有打开Form,显得主界面代码整洁些。

  但是这步出现了个问题,查了资料也没能解决。

  我做的删除功能是通过点击选择想要删除的行,暂时没有输入数据进行删除的功能。但是连接数据库的DataGirlView只在主界面,点击选取表的功能也只能出现在主界面进行,在子界面没办法操作主界面。

  所以删除的代码只好写在了主界面的代码里。

 

    删除语句:

 

string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();        //读取改行第一列的值,即Sno主码

string delete_by_id = "delete from Student where Sno=" + select_id;        //SQL删除语句

 

  注:此处select_id即为读取的Sno主码,将其作为参数,带参函数传至子窗口,即可在子窗口提取select_id使用。

 

  主界面,带参函数:

  

string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
Form6 form6 = new Form6(select_id);

 

  子界面:

 

        public Form6()
        {
            InitializeComponent();
        }

        public Form6(string select_id)      //参数
        {
            this.select_id = select_id;
            InitializeComponent();
        }

 

 

  删除代码:

 

        private void button3_Click(object sender, EventArgs e)          //删
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=******");
            try
            {
                con.Open();
                
                string delete_by_id = "delete from Student where Sno=" + select_id;        //SQL删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("删除成功!请点击“刷新”按钮");
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }

        }

 

结果

 

  删除成功。

 

 

  注:必须点击表行前面、选取全部改行,点击其他位置会错误。

 

 

 

  Form中的结果。

 

 

  SQL中的结果。

 

 


 

 

  思路:修改即为删除+添加,选中一行,将原数据删除再在此行添加新数据。

 

代码

 

  读取选取的该行的第一列,即学号。主界面代码:

        private void button4_Click(object sender, EventArgs e)      //改
        {
            try
            {
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
                Form7 form7 = new Form7(select_id);
                form7.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }

        }

 

 

  子窗口代码:

 

  实例化:

 

SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");

 

            try
            {
                con.Open();     //打开
                string ComStr = "UPDATE Student SET Sname='" + StuName + "',Ssex='" + StuSex + "',Sage=" + StuAge + ",Sdept='" + StuDept + "'" + " WHERE Sno='" + StuNum + "'"; //SQL添加语句
                SqlCommand cmd = new SqlCommand(ComStr, con);
                cmd.ExecuteNonQuery();      //返回执行命令后影响的参数
                MessageBox.Show("修改成功!请点击“刷新”按钮");
                this.Close();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }

 

结果

 

 

 

  Form中的结果。

 

 

  SQL中的结果。

 

 


 

 

  查找的方法比较麻烦,我也没有找到更好的方法。

  出现问题和删除的问题相似,都是现实结果必须在有DataGirdView的主界面写代码,才能现实SQL操作的结果。而填写查找内容在子窗口,为了将传递两个窗口的内容,引入全局变量。

  在子窗口存入查找的内容,在主界面进行SQL操作现实查找的结果。

 

        public static string StuText;
        public static string StuSex;
        public static string Studept;
        public static int flag = -1;

 

界面

 

  新建一个查找方式界面,

 

  

 

 

代码

 

  利用Hide(); 和 Show(); 函数进行隐藏和显示。

  开始按钮“确定”、textBox、和两个comboBox都进行隐藏,点击“”“选择”按钮后,显示“确定”按钮。

 

  以下为子窗口部分代码:

 

        public Form8()
        {
            InitializeComponent();
            textBox1.Hide();
            comboBox2.Hide();
            comboBox3.Hide();
            button3.Hide();
        }

 

  按钮“选择”,更改flag,以进行if选择操作。

  选择“学号”、“姓名”、“年龄”的时候,显示textBox。

  选择“性别”、“系别”的时候,显示comboBox。

 

        private void button1_Click(object sender, EventArgs e)      //选择
        {
            string StuCombo = comboBox1.Text.Trim();
            
            if (comboBox1.Text == "学号")
            {
                flag = 0;
                textBox1.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "姓名")
            {
                flag = 1;
                textBox1.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "性别")
            {
                flag = 2;
                comboBox2.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "年龄")
            {
                flag = 3;
                textBox1.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "系别")
            {
                flag = 4;
                comboBox3.Show();
                button3.Show();
            }

 

  以下为主界面部分代码:

 

  实例化。

 

SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");

 

  按照学号查找为例:

 

            if (Form8.flag == 0)         //按学号查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_id = "select * from Student where Sno='" + Form8.StuText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

 

结果

 

  点击“查找方式”,只显示选择方式,隐藏“确定”按钮。

 

  选择后显示“确定”按钮,以及textBox和comboBox。

 

 

 

 

  点击“确定”后退出,点击“查询”按钮。

 

 

  显示成功。

 

 


 

取消

 

    所有的取消按钮。

        private void buttons2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

 


 

刷新

 

  因为DataGirlView显示的表进行操作后无法立即显示,所以增加了一个“刷新”按钮。

 

  主界面的这段代码,即为刷新的代码。

 

 

            this.studentTableAdapter.Fill(this.sTUDataSet3.Student);
            MessageBox.Show("刷新");

 

  再加上查找功能后,表中只显示查找后的结果,为了刷新表,增加以下代码,将其填到“刷新“”按钮中:

  即SELECT查找全部Student表。

SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                String select_by_id = "select * from Student";

                con.Open();
                SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章