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

📄 yuanxixinxi.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 yuanxixinxi : Form
    {
        public yuanxixinxi()
        {
            InitializeComponent();
        }
        string connstr = "Data Source=localhost;Initial Catalog=mystuInfoDB;Integrated Security= true;";
        bool newRecord = false;
        private void yuanxixinxi_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“mystuInfoDBDataSet4.pro”中。您可以根据需要移动或移除它。
            this.proTableAdapter.Fill(this.mystuInfoDBDataSet4.pro);
            txt_proname.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
            txt_xinxi.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_proname.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
            txt_xinxi.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();

        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            txt_proname.Text = null;
            txt_xinxi.Text = null;
            txt_proname.Focus();
            newRecord = true;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string cmdstr;
            if (newRecord == true)//新记录
            {
                cmdstr = "insert into pro values('" + txt_proname.Text.Trim() +
                  "','" + txt_xinxi.Text + "')";
            }
            else
            {
                cmdstr = "update pro set xinxi='"+txt_xinxi.Text+
                       "' where proname='" + txt_proname.Text.Trim() + "'";
            }

            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;
                tableRefresh();//表刷新
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            string cmdstr;
            cmdstr = "delete from pro where proname='" + txt_proname.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 tableRefresh() //刷新表内数据
        {
            //将表pro重新装入proTableAdapter中
            this.proTableAdapter.Fill(mystuInfoDBDataSet4.pro);
        }

    }
}

⌨️ 快捷键说明

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