c#与数据库的连接(三)登录与注册功能 信息管理系统

项目效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
把管理员的登录放在了panel里。
共有登录,注册,加载,管理 ,修改,管理员操作6个窗体。
登录:

              a.Open();
            string uid = textBox1.Text.Trim();
            string pwd = textBox2.Text.Trim();

          
           
            if (textBox1.Text==""||textBox2.Text=="")
            {
                MessageBox.Show("账号或密码不能为空","提醒");
                a.Close();
            }
           
            else
            {
                  string sqlstr="select uid,pwd from  班级表 where uid = " + uid + "  and pwd = " + pwd ;
                    SqlCommand  sqlCommand = new SqlCommand(sqlstr, a);
                    SqlDataReader dataReader = sqlCommand.ExecuteReader();
            try
            {
                   
                   
                    if(  dataReader.HasRows  )
                    {
                         
                     MessageBox.Show("登录成功","恭喜您");
                     this.Hide();
                      Form1 c = new Form1();
                      c.Show();
                    }
                    else
                    {

                       
                        MessageBox.Show("账号或者密码错误");
                      
                       
                    }
              
                    
                
            }
            catch
            {
                MessageBox.Show("失败");
               
            }
            finally
            {
                  
                a.Close();
                dataReader.Dispose();
                   
            }
            }
           

注册:


        
  if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {

                MessageBox.Show("请把信息填写完整");
            }
            else
            {
                sql.Open();

                string s2 = Convert.ToString(textBox2.Text);
                string s3 = Convert.ToString(textBox3.Text);
                string s4 = Convert.ToString(textBox4.Text);

                string sqladd = "insert into 班级表  (uid,pwd ,name ,class) values  ( " + textBox1.Text + " , ' " + s2 + " ' , '" + s3 + " ' ,'" + s4 + " ' " + " ) ";
                SqlCommand n = new SqlCommand(sqladd, sql);
                try
                {

                    n.ExecuteNonQuery();
                    MessageBox.Show("注册成功!", "恭喜你");

                }
                catch
                {
                    MessageBox.Show("失败");
                }
                finally
                {
                    sql.Close();
                }
            }

修改的bug1:直接在注册界面退出程序:
在formclosing事件中:

DialogResult result = MessageBox.Show("直接退出得到话会直接退出程序", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                Dispose();
                Application.Exit();
            }
            else
            {
                a.Cancel = true;
            }

修改:

            conn.Open();
            string s1 = textBox1.Text;
            string s2 = textBox2.Text;
            string s3 = textBox3.Text;
           
            for (int i = Form1.form1.dataGridView1.SelectedRows.Count; i > 0; i--)
            {
                int ID = Convert.ToInt32(Form1.form1.dataGridView1.SelectedRows[i - 1].Cells[0].Value);
               
                string updata = "update 属性表 set name= '" + s1 + "',sex = '" + s2 + "',occupation = '" + s3 + " ' where id = "+ID;
                SqlCommand sqlupdata = new SqlCommand(updata, conn);
                try
                {
                    sqlupdata.ExecuteNonQuery();
                    MessageBox.Show("成功");
                }
                catch
                {
                    MessageBox.Show("失败");


                }
             
               
            } conn.Close();
           
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值