简易的人员管理~数据的增、删、查、改

              通过sql server 语句对数据进行增删查改实现一个简易的学生管理系统的。

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.Configuration;
using System.Data.SqlClient;

namespace _2012._2._22人员管理
{
    public partial class Manager : Form
    {
        public Manager()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            string str = "select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where ";
            //cmd.CommandText="select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where "+comboBox1.Text+" like '%"+textBox1.Text+" %' ";
            string strs = ReturnSqlWhere(cmd);
            cmd.CommandText = str + strs;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                this.dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("数据不存在!");
            }
        }
        private string ReturnSqlWhere(SqlCommand cmd)
        {
            string sqlwhere = string.Empty;
            switch (comboBox1.Text)
            {
                case "姓名":
                    sqlwhere = "B.Name like @name";
                    cmd.Parameters.AddWithValue("@name", "%" + textBox1.Text + "%");
                    break;
                case "用户名":
                    sqlwhere = "A.UserName like @username";
                    cmd.Parameters.AddWithValue("@username", "%" + textBox1.Text + "%");
                    break;
                case "电话":
                    sqlwhere = "B.Tell like @tell"; cmd.Parameters.AddWithValue("@tell", "%" + textBox1.Text + "%");
                    break;
                case "地址":
                    sqlwhere = "B.Address like @address";
                    cmd.Parameters.AddWithValue("@address", "%" + textBox1.Text + "%");
                    break;
                default:
                    sqlwhere = "B.Sfzh like @sfzh";
                    cmd.Parameters.AddWithValue("@sfzh", "%" + textBox1.Text + "%");
                    break;
            }
            return sqlwhere;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            add a = new add();
            a.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            int cou =Convert.ToInt32(this.dataGridView1.SelectedCells[0].Value);
            cmd.CommandText = "update T_User set Error=0 where UserId=@userid";
            cmd.Parameters.AddWithValue("@userid",cou);
            if (cmd.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("解锁成功");
            }
            else
            {
                MessageBox.Show("解锁失败");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            string str = "select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where ";
            //cmd.CommandText="select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where "+comboBox1.Text+" like '%"+textBox1.Text+" %' ";
            string strs = ReturnSqlWhere(cmd);
            cmd.CommandText = str + strs;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                this.dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("数据不存在!");
            }
        }
    }
}

以上包括查和增,还有对用户的解锁,当一个用户登陆时三次错误密码,账户就会在一定被锁定,这时可以找管理员进行解锁。解锁就是对数据库中的数据进行修改,所以以上代码基本就包括了数据库的操作增、查、改!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值