C#与数据库的增删改查以及登录功能实现

  • 增删改查界面功能:
  1. 删:选择要删除的行进行删除操作。
  2. 改:选择要修改的行,在左侧进行修改,修改完成后点击修改按钮即可进行数据库的更新。
  3. 查:全部查询和关键字查询

 

界面代码实现:

namespace Test01.View
{
    public partial class StaffInfo : System.Web.UI.Page
    {
        Model.Functions con;
        //界面加载时
        protected void Page_Load(object sender, EventArgs e)  
        {
            con = new Model.Functions();
            ShowMessage();
        }
        //将数据库中的数据显示在GridView控件上(id=staffInfoGV)
        private void ShowMessage()
        {
            string sqlQuery = "select UId as 编号,UName as 姓名,UDepartment as 部门,UAddress as 邮箱,UPhone as 电话 from UserTable";
            staffInfoGV.DataSource = con.GetAllData(sqlQuery);
            staffInfoGV.DataBind();
        }
        //条件查询
        private void QuerybyNameOrDepartment(string name, string department)
        {
            string sqlQuery = "select UId as 编号,UName as 姓名,UDepartment as 部门,UAddress as 邮箱,UPhone as 电话 from UserTable where ";
            //根据查询条件进行查询
            if (name != "" && department != "")
            {
                sqlQuery += "UName like '%{0}%' and UDepartment like '%{1}%'";
                sqlQuery = string.Format(sqlQuery, name, department);
            }
            else if (name != "")
            {
                sqlQuery += "UName like '%{0}%'";
                sqlQuery = string.Format(sqlQuery, name);
            }
            else {
                sqlQuery += "UDepartment like '%{0}%'";
                sqlQuery=string.Format(sqlQuery, department);
            }
            staffInfoGV.DataSource = con.GetAllData(sqlQuery);
            staffInfoGV.DataBind();

        }
        //点击查询
        protected void querybtn_Click(object sender, EventArgs e)
        {
            if(nameQuery.Value=="" && departmentQuery.Value == "")//全部查询
            {
                ShowMessage();
            }
            else
            {
                QuerybyNameOrDepartment(nameQuery.Value,departmentQuery.Value);
            }
            
        }

        //信息插入
        protected void insertBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string name = nametxt.Value;
                string department = departmenttxt.Value;
                string email = emailtxt.Value;
                string phone=phonetxt.Value;
                string sqlInsert = "insert into UserTable values('{0}','{1}','{2}','{3}')";
                sqlInsert =string.Format(sqlInsert,name,department,email,phone); 
                con.SetData(sqlInsert);
                ShowMessage();
                ErrMsg.InnerText = "添加成功";
            }
            catch (Exception ex)
            {

                ErrMsg.InnerText = ex.ToString();
            }
        }

        //选中GridView中的某条信息,将其显示在页面左侧的文本框中
        protected void staffInfoGV_SelectedIndexChanged(object sender, EventArgs e)
        {
            //key = Convert.ToInt32(staffInfoGV.SelectedRow.Cells[1].Text);
            nametxt.Value=staffInfoGV.SelectedRow.Cells[2].Text;
            departmenttxt.Value=staffInfoGV.SelectedRow.Cells[3].Text;
            emailtxt.Value=staffInfoGV.SelectedRow.Cells[4].Text;
            phonetxt.Value=staffInfoGV.SelectedRow.Cells[5].Text;
        }

        //修改
        protected void alterBtn_Click(object sender, EventArgs e)
        {
            string sqlupdate = "update UserTable set UName='{0}',Udepartment='{1}',UAddress='{2}',UPhone='{3}' where UId='{4}'";
            sqlupdate = string.Format(sqlupdate, nametxt.Value, departmenttxt.Value, emailtxt.Value, phonetxt.Value, staffInfoGV.SelectedRow.Cells[1].Text);
            con.SetData(sqlupdate);
            ShowMessage();
            ErrMsg.InnerText = "修改成功";
        }
        //删除
        protected void deleteBtn_Click(object sender, EventArgs e)
        {
            string sqlDel = "delete from UserTable where UName='{0}' and UDepartment='{1}' and UAddress='{2}' and UPhone='{3}'";
            sqlDel = string.Format(sqlDel, nametxt.Value, departmenttxt.Value, emailtxt.Value, phonetxt.Value);
            con.SetData(sqlDel);
            ShowMessage();
            ErrMsg.InnerText = "删除成功";
        }
        //重置
        protected void resetBtn_Click(object sender, EventArgs e)
        {
            nametxt.Value = "";
            departmenttxt.Value = "";
            emailtxt.Value = "";
            phonetxt.Value = "";
        }
    }
}

 

  •  登录界面

 登录模块功能实现代码如下:

namespace Test01.View
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Model.Functions con;
        protected void Page_Load(object sender, EventArgs e)
        {
            con=new Model.Functions();
        }

        protected void loginBtn_Click(object sender, EventArgs e)
        {
            if(nameInput.Value=="" || passwordInput.Value == "")
            {
                ErrMsg.InnerText = "用户名和密码不能为空!";
            }
            else
            {
                string sqlLogin = "select * from LoginTable where Name='{0}' and Password='{1}'";
                sqlLogin=String.Format(sqlLogin, nameInput.Value,passwordInput.Value);
                if (con.Login(sqlLogin))
                {
                    Response.Redirect("StaffInfo.aspx");
                }
                else
                {
                    ErrMsg.InnerText = "用户名或密码错误!";
                }

            }

        }
        protected void resetBtn_Click(object sender, EventArgs e)
        {
            nameInput.Value = "";
            passwordInput.Value = "";
        }
    }
}

 与数据库交互部分:

using System.Data.SqlClient;
using System.Data;

namespace Test01.Model
{
    public class Functions
    {
        private SqlConnection _connection;
        private SqlCommand _command;
        private SqlDataAdapter _adapter;
        private DataTable _data;
        private string constr;
        public Functions()  //构造函数
        {
            constr = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\Documents\\Data.mdf;Integrated Security=True;Connect Timeout=30";      //数据库连接字符串
            _connection = new SqlConnection(constr); //新建数据库连接
            _command = new SqlCommand();
            _command.Connection = _connection; 
        }
        public DataTable GetAllData(string sqlQuery) //查询数据库中的数据
        {
            _data = new DataTable();
            _adapter=new SqlDataAdapter(sqlQuery,constr);
            _adapter.Fill(_data);
            return _data;
        }
        public int SetData(string sqlInsert)//数据库更新,包括增、删、改
        {
            int i = 0;
            if (_connection.State == ConnectionState.Closed)
            {
                _connection.Open();
            }
            _command.CommandText = sqlInsert;
            i=_command.ExecuteNonQuery();
            _connection.Close();
            return i;
        }
        public bool Login(string sqlLogin) //验证数据是否存在select * from table where name='{0}' and pwd='{1}';
        {
            bool flag = false;
            if (_connection.State == ConnectionState.Closed)
            {
                _connection.Open();
            }
            _command.CommandText = sqlLogin;
            SqlDataReader reader = _command.ExecuteReader(); //快速查询返回sqlDataReader实例
            if (reader.HasRows) //判断返回数据是否为空
            {
                flag = true;
            }
            _connection.Close();
            return flag;
        }

    }
}

  • 1
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值