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

📄 studentinfo.cs

📁 利用vs2008+sql2000开发的小型学生信息系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace StudentInfoMS
{
    public partial class studentInfo : Form
    {
        string connstr = "Data Source=localhost;Initial Catalog=mystuInfoDB;Integrated Security=True";
        bool newRecord = false;
        public studentInfo()
        {
            InitializeComponent();
        }

        private void studentInfo_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“mystuInfoDBDataSet.studentInfo”中。您可以根据需要移动或移除它。
            this.studentInfoTableAdapter.Fill(this.mystuInfoDBDataSet.studentInfo);
            txt_sid.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
            txt_name.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();
            txt_sex.Text = dataGridView1.Rows[0].Cells[2].Value.ToString();
            txt_minzu.Text = dataGridView1.Rows[0].Cells[3].Value.ToString();
            txt_jiguan.Text = dataGridView1.Rows[0].Cells[4].Value.ToString();
            txt_birthday.Text = dataGridView1.Rows[0].Cells[5].Value.ToString();
            txt_homeaddress.Text = dataGridView1.Rows[0].Cells[6].Value.ToString();
            txt_postcode.Text = dataGridView1.Rows[0].Cells[7].Value.ToString();

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_sid.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
            txt_name.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
            txt_sex.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
            txt_minzu.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
            txt_jiguan.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
            txt_birthday.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
            txt_homeaddress.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
            txt_postcode.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value.ToString();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            txt_sid.Text = null;
            txt_name.Text = null;
            txt_sex.Text = null;
            txt_minzu.Text = null;
            txt_jiguan.Text = null;
            txt_birthday.Text = null;
            txt_homeaddress.Text = null;
            txt_postcode.Text = null;
            newRecord = true;
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            //将用户修改表中的数据保存
            this.Validate();
            this.studentInfoBindingSource.EndEdit();
            this.studentInfoTableAdapter.Update(mystuInfoDBDataSet.studentInfo);
            tableRefresh();//刷新表中数据
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (txt_sid.Text.Trim().ToString().Equals("") == false)
            {
                string cmdstr;
                if (newRecord == true)//新记录
                {
                    cmdstr = "insert into studentinfo values('" + txt_sid.Text.Trim() +
                     "','" + txt_name.Text.Trim() +
                     "','" + txt_sex.Text.Trim() +
                     "','" + txt_minzu.Text.Trim() +
                     "','" + txt_jiguan.Text.Trim() +
                     "','" + txt_birthday.Text.Trim() +
                     "','" + txt_homeaddress.Text.Trim() +
                     "','" + txt_postcode.Text + "')";
                }
                else
                {
                    cmdstr = "update studentinfo set sname='" + txt_name.Text +
                          "',sex='" + txt_sex.Text +
                          "',minzu='" + txt_minzu.Text +
                          "',jiguan='" + txt_jiguan.Text +
                          "',birthday='" + txt_birthday.Text +
                          "',homeaddress='" + txt_homeaddress.Text +
                          "',postcode='" + txt_postcode.Text +
                          "' where sid='" + txt_sid.Text + "'";
                }

                SqlConnection myconnection = new SqlConnection(connstr);
                SqlCommand sqlcmd = new SqlCommand(cmdstr, myconnection);
                try
                {
                    myconnection.Open();
                    int row = sqlcmd.ExecuteNonQuery();
                    myconnection.Close();
                    if (row == 0)
                    {
                        MessageBox.Show("请重新检查输入和数据库连接!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                }
                finally
                {
                    myconnection.Close();
                    newRecord = false;
                    //将用户修改表中的数据保存
                    this.Validate();
                    this.studentInfoBindingSource.EndEdit();
                    this.studentInfoTableAdapter.Update(mystuInfoDBDataSet.studentInfo);
                    tableRefresh();//刷新表
                }
            }
            else
            {
                MessageBox.Show("请输入学号!");
                txt_sid.Focus();
            }

        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            string cmdstr;
            cmdstr = "delete from studentinfo where sid='" + txt_sid.Text.Trim()+"'";
            SqlConnection mysqlconnection = new SqlConnection(connstr);
            SqlCommand sqlcmd = new SqlCommand(cmdstr, mysqlconnection);
            if (MessageBox.Show("确定删除该学生信息?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                 try
                 {
                     mysqlconnection.Open();
                     int row=sqlcmd.ExecuteNonQuery();
                    if (row == 0)
                    {
                          MessageBox.Show("请重新检查输入和数据库连接!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                   else
                    {
                      MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                 }
                catch (Exception ex)
                 {
                     MessageBox.Show("请重新检查输入和数据库连接!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 }
                finally
               {
                   mysqlconnection.Close();
                   tableRefresh();
               }
            }

        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void tableRefresh() //刷新表内数据
        {
            //将表studentInfo重新装入studentInfoTableAdapter中
            this.studentInfoTableAdapter.Fill(mystuInfoDBDataSet.studentInfo);
        }

    }
}

⌨️ 快捷键说明

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