码迷,mamicode.com
首页 > 其他好文 > 详细

学生信息管理系统

时间:2018-03-04 19:10:22      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:string   where   依次   using   src   read   forms   跳转   lda   

*登入代码*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.SqlClientnamespace studentwork
{
    public partial class FrmLogin : Form
    {
        static public string sn;
        public FrmLogin()
        {
            InitializeComponent();
        }
        private static string connStr = "Server=.;Database=学生工作管理系统;user id=sa;password=0000";
        //首先 设置 连接到配置文件组建 App.config
        //当然 这里的 ConfigurationManager 系统是默认不使用其命名空间的 因此需要解析
        //string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;        private void btnLogin_Click(object sender, EventArgs e)
        {
            //使用SqlConnection 来连接数据库
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                //创建sql 查询语句 
                string sql = "select 密码 from 用户 where 用户名 =‘" + txtName.Text + "‘";
                //创建 SqlCommand 执行指令
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    //打开数据库连接
                    conn.Open();
                    if (txtName.Text == "" || txtPwd.Text == "")
                        MessageBox.Show("信息不全,请不要遗漏信息!");
                    //使用 SqlDataReader 来 读取数据库 
                    if (rbtnteacher.Checked)
            {
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    //SqlDataReader 在数据库中为 从第1条数据开始 一条一条往下读
                    if (sdr.Read())  //如果读取账户成功(文本框中的用户名在数据库中存在)
                    {

                        //则将第1条 密码 赋给 字符串pwd  ,并且依次往后读取 所有的密码
                        //Trim()方法为移除字符串前后的空白
                        string pwd = sdr.GetString(0).Trim();
                        //如果 文本框中输入的密码 ==数据库中的密码
                        if (pwd == txtPwd.Text)
                        {
                            //说明在该账户下 密码正确, 系统登录成功
                            MessageBox.Show("系统登录成功,正在跳转主页面...");
                            主页面 manager = new 主页面();
                            manager.Show();
                            this.Hide();
                        }
                        else
                        {
                            //否则密码错误 再次输入密码
                            MessageBox.Show("密码或用户名出错!请再次输入!");
                            //并自动将当前密码 清空
                            txtName.Text = ""; txtPwd.Text = "";
                        }
                    }         
                }
            }


                    if (rbtnstudent.Checked)
                    {
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                           
                            if (sdr.Read())
                            {
                               
                                string pwd = sdr.GetString(0).Trim();
                               
                                if (pwd == txtPwd.Text)
                                {
                                   
                                    MessageBox.Show("系统登录成功,正在跳转成绩页面...");
                                    成绩 manager = new 成绩();
                                    manager.Show();
                                    this.Hide();
                                }
                                else
                                {
                                    //否则密码错误 再次输入密码
                                    MessageBox.Show("密码或用户名出错!请再次输入!");
                                    //并自动将当前密码 清空
                                    txtName.Text = ""; txtPwd.Text = "";
                                }
                            }
                           
                        }
                    }
                }
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }


    }
}

 

 

 

界面

技术分享图片

 

学生信息管理系统

标签:string   where   依次   using   src   read   forms   跳转   lda   

原文地址:https://www.cnblogs.com/xiaoxiyou/p/8505423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!