美文网首页
第三次实验课

第三次实验课

作者: S_s_s_a53f | 来源:发表于2017-09-21 17:07 被阅读0次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConnection mycon = new SqlConnection(strCon);
                mycon.Open();


                string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";
                SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
                DataTable dt = new DataTable();
                myda.Fill(dt);
                dataGridView1.DataSource = dt;


                mycon.Close();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "增加")
            {
                try
                {
                    // string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
                    string cno = textcno.Text;
                    string cname = textcname.Text;
                    string Ccode = textcode.Text;

                    SqlConnection mycon = new SqlConnection(strCon);
                    mycon.Open();



                    //string StrUpd = "update T_Student set sage='" + sage + "' where sname='" + sname + "'";
                    string strins = "insert into T_course(cno,cname,Ccode) values ('" + cno + "','" + cname + "','" + Ccode + "')";
                    //string StrUpd = "insert into T_Student(Sno,Sname,Ssex,Sage,Sdept)  values ('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sdept+"')";

                    SqlCommand mycmd = new SqlCommand(strins, mycon);
                    mycmd.ExecuteNonQuery();

                    string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";

                    SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
                    DataTable dt = new DataTable();
                    myda.Fill(dt);
                    dataGridView1.DataSource = dt;



                    mycon.Close();
                    MessageBox.Show("增加成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                try
                {
                    // string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
                    string cno = textcno.Text;
                    string cname = textcname.Text;
                    string Ccode = textcode.Text;

                    SqlConnection mycon = new SqlConnection(strCon);
                    mycon.Open();



                    string StrUpd = "update T_course set cname='" + cname + "' , Ccode='" + Ccode + "' where  cno='" + cno + "'";
                    //string strins = "insert into T_course(cno,cname,Ccode) values ('" + cno + "','" + cname + "','" + Ccode + "')";
                    //string StrUpd = "insert into T_Student(Sno,Sname,Ssex,Sage,Sdept)  values ('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sdept+"')";

                    SqlCommand mycmd = new SqlCommand(StrUpd, mycon);
                    mycmd.ExecuteNonQuery();

                    string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";

                    SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
                    DataTable dt = new DataTable();
                    myda.Fill(dt);
                    dataGridView1.DataSource = dt;



                    mycon.Close();
                    MessageBox.Show("修改成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                textcno.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                textcname.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                textcode.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                button1.Text = "修改";
                textcno.Enabled = false;
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show("确定要删除这条记录吗?","", MessageBoxButtons.YesNo))
                {
                    // string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
                    //string cno = textcno.Text;
                    //string cname = textcname.Text;
                    //string Ccode = textcode.Text;

                    SqlConnection mycon = new SqlConnection(strCon);
                    mycon.Open();
                    string cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    string strins = "delete from T_course where cno ='"+cno+"'";



                    //string StrUpd = "update T_course set cname='" + cname + "' , Ccode='" + Ccode + "' where  cno='" + cno + "'";
                    //string strins = "insert into T_course(cno,cname,Ccode) values ('" + cno + "','" + cname + "','" + Ccode + "')";
                    //string StrUpd = "insert into T_Student(Sno,Sname,Ssex,Sage,Sdept)  values ('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sdept+"')";

                    SqlCommand mycmd = new SqlCommand(strins, mycon);
                    mycmd.ExecuteNonQuery();

                    string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";

                    SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
                    DataTable dt = new DataTable();
                    myda.Fill(dt);
                    this.dataGridView1.DataSource = dt;



                    mycon.Close();
                    MessageBox.Show("删除成功!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
    }

}
    

相关文章

  • 第三次实验课

  • 科学小实验课堂第三次课

    在文博社区开展了两次共4节课的活动后,我带着更多的反思和提升将到科大社区开展活动。鉴于孩子们的反响,感受到他们觉得...

  • 泰安市实验学校西校区物理组课例研究(三)

    9月20日下午物理组进行了开学以来的第三次课例研究展示,执教人耿继成老师,地点:物理实验室,执教课题题目《...

  • 实验课

    今天我们在学校的报告厅上了一节大课,是化学实验课,很有意思,同学们看的都瞪大了眼珠。 老师将小...

  • 实验课

    今天下午两点,我和爸爸、妈妈、同学们,还有同学们的爸爸和妈妈一起上实验课。 上课了,我看见了一团团的东...

  • 实验课

    今天就要完成作业了

  • 实验课

    大二第一节实验课 取土

  • 实验课

    他们说在水中放进一块小小的明矾 就能沉淀出所有的渣滓 那么如果在我们的心中放进一首诗 是不是也可以沉淀出所有的昨日...

  • 实验课

    整天呆在实验室,做实验做到想吐,站到脚疼,这就是我们一天的生活,好累!!!

  • 实验课

    今天上了植物学的实验课 感觉超级好玩 那个叶绿体最好看啦 小小的一颗一颗的 晶莹的 感觉很可爱呢 手绘卡在了画树那...

网友评论

      本文标题:第三次实验课

      本文链接:https://www.haomeiwen.com/subject/dqqasxtx.html