⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 course.cs

📁 学生管理(包括学生成绩 档案和课程) 学生可以录入数据
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManager
{
    public partial class Course : Form
    {
        private SqlConnection conn1 = null;
        private SqlDataAdapter DataAdapter = null;
        private string selectstr = null;
        private DataSet Dataset = new DataSet();
        private SqlCommand command1 = null;
        private string  commandstr=null;
        private SqlDataReader DataReader=null;
        public Course()
        {
            InitializeComponent();
            selectstr = "Select * from Course";
            conn1 = new SqlConnection(DataLevel.Connection.ConnString);
            DataAdapter = new SqlDataAdapter(selectstr, this.conn1);
            command1 = new SqlCommand();
            command1.Connection = conn1;
        }

        private void Course_Load(object sender, EventArgs e)
        {
            this.conn1.Open();
            this.DataAdapter.Fill(this.Dataset, "Course");
            this.Data_Bindings();
            this.Combo_fill();
        }
        private void Combo_fill()
        {
            if (conn1.State == ConnectionState.Closed)
                conn1.Open();
            commandstr = "Select Department from Course";
            command1.CommandText = commandstr;
            DataReader = command1.ExecuteReader();
            while (DataReader.Read())
            {
                this.comboBoxDepartment.Items.Add(this.DataReader["Department"].ToString());
            }
            this.DataReader.Close();
            this.conn1.Close();
            this.button_contro(false);

        }
        private void Data_Bindings()
        {
            this.dataGridView1.DataSource = this.Dataset;
            this.dataGridView1.DataMember = "Course";
            this.textBoxID.DataBindings.Add("Text", this.Dataset, "Course.CourseID");
            this.textBoxHour.DataBindings.Add("Text", this.Dataset, "Course.ScoreHour");
            this.textBoxName.DataBindings.Add("Text",this.Dataset,"Course.CourseName");
            this.comboBoxDepartment.DataBindings.Add("Text", this.Dataset, "Course.Department");
        }
        private void button_contro(bool Isvalid)
        {
            if (Isvalid)
            {
                this.btnApply.Enabled = true;
                this.btnCancle.Enabled = true;
                this.textBoxHour.Enabled = true;
                this.textBoxName.Enabled = true;
                this.textBoxID.Enabled = true;
                this.comboBoxDepartment.Enabled = true;
            }
            else
            {
                this.btnApply.Enabled = false ;
                this.btnCancle.Enabled = false ;
                this.textBoxHour.Enabled = false ;
                this.textBoxName.Enabled = false ;
                this.textBoxID.Enabled = false ;
                this.comboBoxDepartment.Enabled = false ;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.button_contro(true);
            this.BindingContext[this.Dataset, "Course"].AddNew();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            button_contro(true);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if ((this.BindingContext[this.Dataset, "Course"].Count > 0) &&
               (MessageBox.Show("真的要删除此记录吗", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question).Equals(DialogResult.OK)))
            {
                int position = this.BindingContext[this.Dataset, "Course"].Position;
                this.BindingContext[this.Dataset, "Course"].RemoveAt(position);
            }
            try
            {
                this.BindingContext[this.Dataset, "Course"].EndCurrentEdit();
                if (this.conn1.State == ConnectionState.Closed)
                    conn1.Open();
                SqlCommandBuilder Commandbuiter = new SqlCommandBuilder(this.DataAdapter);
                this.DataAdapter.Fill(this.Dataset, "Course");
                this.dataGridView1.Refresh();
                this.Dataset.AcceptChanges();
            }
            catch (Exception E)
            {
                MessageBox.Show(E.ToString());
            }
            finally
            {
                this.conn1.Close();
                button_contro(false);
            }
            
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.BindingContext[this.Dataset, "Course"].EndCurrentEdit();
            button_contro(false);
           
        }

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

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.BindingContext[this.Dataset,"Course"].EndCurrentEdit();
            if (conn1.State == ConnectionState.Closed)
                conn1.Open();
            SqlCommandBuilder Commandbuiter = new SqlCommandBuilder(this.DataAdapter);
            this.DataAdapter.Fill(this.Dataset, "Course");
            this.Dataset.AcceptChanges();
            this.dataGridView1.Refresh();
            button_contro(false);
            conn1.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -