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

📄 form4.cs

📁 vs2008 组织部信息系统
💻 CS
字号:
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 团委组织部信息系统
{
    public partial class Form4 : Form
    {
        SqlDataAdapter da;
        DataSet ds;
        string connectionstring = @"data source=localhost;Initial catalog=tuanweizuzhibu_inf;integrated security=SSPI;";
        string commandstring = @"select * from tuanweigebu_inf ";
        public Form4()
        {
            InitializeComponent();
        }

        private void tuanweigebu_infBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tuanweigebu_infBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.tuanweizuzhibu_infDataSet);

        }

        private void Form4_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tuanweizuzhibu_infDataSet.tuanweigebu_inf' table. You can move, or remove it, as needed.
            this.tuanweigebu_infTableAdapter.Fill(this.tuanweizuzhibu_infDataSet.tuanweigebu_inf);

        }

        private void button6_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection;
            String connString = "Data Source=.;Initial Catalog= tuanweizuzhibu_inf; Integrated Security= True";
            sqlConnection = new System.Data.SqlClient.SqlConnection(connString);
            int i = comboBox2.SelectedIndex;
            SqlCommand sqlcmd = new SqlCommand();
            switch (i)
            {
                case 0: sqlcmd.CommandText = "select * from tuanweigebu_inf where 学号= @ID"; break;
                case 1: sqlcmd.CommandText = "select * from tuanweigebu_inf where 姓名= @ID"; break;
                case 2: sqlcmd.CommandText = "select * from tuanweigebu_inf where 学院= @ID"; break;
                case 4: sqlcmd.CommandText = "select * from tuanweigebu_inf where 班级= @ID"; break;
                case 5: sqlcmd.CommandText = "select * from tuanweigebu_inf where 所属部门= @ID"; break;
                case 6: sqlcmd.CommandText = "select * from tuanweigebu_inf where 担任职务= @ID"; break;
                case 7: sqlcmd.CommandText = "select * from tuanweigebu_inf where 手机号码= @ID"; break;
            }
            sqlcmd.Parameters.AddWithValue("@ID", textBox2.Text);
            sqlConnection.Open();
            sqlcmd.Connection = sqlConnection;
            SqlDataAdapter sa = new SqlDataAdapter();
            sa.SelectCommand = sqlcmd;
            DataSet ds = new DataSet();
            sa.Fill(ds, "tuanweigebu_inf");
            tuanweigebu_infDataGridView.DataSource = ds.Tables["tuanweigebu_inf"];
           
            sqlConnection.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button5_Click(object sender, EventArgs e)
        {

            学号TextBox.Text = "";
            姓名TextBox.Text = "";
            学院TextBox.Text = "";
            班级TextBox.Text = "";
            所属部门TextBox.Text = "";
            担任职务TextBox.Text = "";
            手机号码TextBox.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (学号TextBox.Text == "")
            { MessageBox.Show(" 学号不能为空!"); }
            else
            {

                SqlConnection conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand(commandstring, conn);
                SqlDataReader dr;
                int prime = 0;
                conn.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.GetString(0) == 学号TextBox.Text)
                    {
                        MessageBox.Show("学号已存在,不能添加相同个人的信息!");
                        prime = 1;
                        break;
                    }
                }
                if (prime == 0)
                    try
                    {
                        da = new SqlDataAdapter(commandstring, connectionstring);
                        SqlCommandBuilder scb = new SqlCommandBuilder(da);
                        ds = new DataSet();
                        da.Fill(ds, "tuanweigebu_inf");
                        DataTable mytable = ds.Tables["tuanweigebu_inf"];
                        //tuanweigebu_infDataGridView.DataSource = ds.Tables["stores"];
                        DataRow myrow = ds.Tables["tuanweigebu_inf"].NewRow();
                        myrow[0] = 学号TextBox.Text.ToString();
                        myrow[1] = 姓名TextBox.Text.ToString();
                        myrow[2] = 学院TextBox.Text.ToString();
                        myrow[3] = 班级TextBox.Text.ToString();
                        myrow[4] = 所属部门TextBox.Text.ToString();
                        myrow[5] = 担任职务TextBox.Text.ToString();
                        myrow[6] = 手机号码TextBox.Text.ToString();


                        mytable.Rows.Add(myrow);
                        da.Update(ds, "tuanweigebu_inf");
                        ds.AcceptChanges();
                        MessageBox.Show("添加成功!");
                        tuanweigebu_infDataGridView.DataSource = ds.Tables["tuanweigebu_inf"];
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("添加失败:" + ex.Message, "出现错误",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        dr.Close();
                        conn.Close();
                    }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (学号TextBox.Text == "" || 姓名TextBox.Text == "" || 学院TextBox.Text == "" || 班级TextBox.Text == "" || 所属部门TextBox.Text == "" || 所属部门TextBox.Text == "" || 手机号码TextBox.Text == "" )
            { MessageBox.Show("请选择要删除的对象!"); }
            else
            {
                da = new SqlDataAdapter(commandstring, connectionstring);
                SqlCommandBuilder scb = new SqlCommandBuilder(da);
                ds = new DataSet();
                da.Fill(ds, "tuanweigebu_inf");
                tuanweigebu_infDataGridView.DataSource = ds.Tables["tuanweigebu_inf"];
                DataTable mytable = ds.Tables["tuanweigebu_inf"];
                mytable.Rows[tuanweigebu_infDataGridView.CurrentRow.Index].Delete();
                da.Update(ds, "tuanweigebu_inf");
                ds.AcceptChanges();
                MessageBox.Show("信息删除成功!");
                tuanweigebu_infDataGridView.DataSource = ds.Tables["tuanweigebu_inf"];
                学号TextBox.Text = "";
                姓名TextBox.Text = "";
                学院TextBox.Text = "";
                班级TextBox.Text = "";
                所属部门TextBox.Text = "";
                担任职务TextBox.Text = "";
                手机号码TextBox.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connectionstring);
            string sqlStatement = "UPDATE tuanweigebu_inf SET " +
                    "学号='" + 学号TextBox.Text + "'," +
                    "姓名='" + 姓名TextBox.Text + "'," +
                    "学院='" + 学院TextBox.Text + "'" +
                    "班级='" + 班级TextBox.Text + "'" +
                    "所属部门='" + 所属部门TextBox.Text + "'" +
                    "担任职务='" + 担任职务TextBox.Text + "'" +
                    "手机号码='" + 手机号码TextBox.Text + "'" +
                    "WHERE 学号 = '" + 学号TextBox.Text + "'";
            SqlCommand cmd = new SqlCommand(sqlStatement, conn);
            try
            {
                conn.Open();
                int rowAffected = cmd.ExecuteNonQuery();
                if (rowAffected == 1)
                {
                    da = new SqlDataAdapter(commandstring, connectionstring);
                    ds = new DataSet();
                    da.Fill(ds, "tuanweigebu_inf");
                    MessageBox.Show("修改成功!");
                    tuanweigebu_infDataGridView.DataSource = ds.Tables["tuanweigebu_inf"];
                    学号TextBox.Text = "";
                    姓名TextBox.Text = "";
                    学院TextBox.Text = "";
                    班级TextBox.Text = "";
                    所属部门TextBox.Text = "";
                    担任职务TextBox.Text = "";
                    手机号码TextBox.Text = "";
                }

            }

            catch (SqlException ex)
            {
                MessageBox.Show("修改失败:" + ex.Message, "出现错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

                conn.Close();
            }

        }

        private void tuanweigebu_infDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            学号TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
            姓名TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
            学院TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
            班级TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
            所属部门TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
            担任职务TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[5].Value.ToString();
            手机号码TextBox.Text = tuanweigebu_infDataGridView.Rows[e.RowIndex].Cells[6].Value.ToString();
            
        }
    }
}

⌨️ 快捷键说明

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