dotnet基本数据操作

 

namespace _2011_07_23
{
    public partial class MainFrm : Form
    {
        SqlConnection conn = BaseClass.ConnDB();
        public void GetInfo()
        {
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select userno as '员工编号',username as '员工姓名',phonenumber as '联系方式',failtimes as '不及格次数' ,ID as '系统编号' from T_Users where usertype=0", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }
        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Activated(object sender, EventArgs e)
        {
            GetInfo();
           
        }

        private void btnAdd_Click(object sender, EventArgs e)
      {
            if (txtEmpNO.Text == "" || txtName.Text == "" || txtPhone.Text == "")
            {
                MessageBox.Show("请将数据填写完整");
            }
            else
            {
                string sql = "insert into T_Users(userno,username,usertype,department,failtimes,password,phonenumber) values('" + txtEmpNO.Text.Trim() + "','" + txtName.Text.Trim() + "'," + "0,'c#',0,'123456'," + "'" + txtPhone.Text.Trim() + "')";
                BaseClass.InsertData(sql);
                txtEmpNO.Text = "";
                txtName.Text = "";
                txtPhone.Text = "";
                GetInfo();
            }
       }

    

        private void btnMod_Click(object sender, EventArgs e)
        {
            panelAdd.Visible = false;
            panelMod.Visible = true;
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("你未选中任何行");
            }
            else
            {
                int m_id = Convert.ToInt32(dataGridView1.SelectedCells[5].Value);
                string sql = "select * from T_Users where ID=" + m_id;
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                textBoxNO.Text = sdr["userno"].ToString();
                textBoxName.Text = sdr["username"].ToString();
                textBoxPhone.Text = sdr["phonenumber"].ToString();
                conn.Close();
            }
         }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            panelAdd.Visible = true;
            panelMod.Visible =false;
        }

        private void btnAd_Click(object sender, EventArgs e)
        {
            panelAdd.Visible = true;
            panelMod.Visible = false;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int m_id = Convert.ToInt32(dataGridView1.SelectedCells[5].Value);
            string sql = "update T_Users set userno='"+textBoxNO.Text.Trim()+"',username='"+textBoxName.Text.Trim()+"',phonenumber='"+textBoxPhone.Text.Trim()+"'where ID="+m_id;
            BaseClass.UpdateData(sql);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            bool check=false;
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                check = Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value);
                if (check)  //判断该复选框是否被选中
                {
                 
                    arr.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value.ToString()));
                }
            }
            if (arr.Count == 0)
            {
                MessageBox.Show("请选择你要删除的员工");
            }
            else
            {
                DialogResult ds = MessageBox.Show("确定要删除吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (ds == DialogResult.Yes)
                {
                    foreach (int org_id in arr)
                    {
                        string sql = "delete from T_Users where ID=" + org_id;
                        conn.Open();
                        SqlCommand cmd = new SqlCommand(sql,conn);
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                    GetInfo();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值