| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 361 人关注过本帖
标题:[请教高手]帮我看看代码那里有问题
只看楼主 加入收藏
thanklife
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-9-17
收藏
 问题点数:0 回复次数:1 
[请教高手]帮我看看代码那里有问题
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MySchool
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            loginType();
        }

        private void loginType()
        {
            DBHelper db = new DBHelper();
            string selectSql = "select type from logintype";
            SqlDataReader reader = db.QueryRs(selectSql);
            while (reader.Read())
            {
                cboType.Items.Add(reader.GetString(0));
            }
            reader.Close();
            db.Close();
        }
        public bool ValidateInput()
        {
            if (txtId.Text == string.Empty)
            {
                MessageBox.Show("请输入用户名","输入提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtId.Focus();
                return false;
            }
            else if (txtPwd.Text == string.Empty)
            {
                MessageBox.Show("请输入密码","输入提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtPwd.Focus();
                return false;
            }
            else if (cboType.Text == string.Empty)
            {
                MessageBox.Show("请选择登陆类型", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboType.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }
        public bool ValidateUser(string loginId, string loginPwd, string loginType, ref string message)
        {
            
            
            DBHelper db = new DBHelper();
            if (loginType == "学员")
            {
                string selectSql = string.Format("select count(*) from Student where loginId='{0}' and loginPwd='{1}'", loginId, loginPwd);
                try
                {
                    
                    db.GetCon();
                    SqlCommand sqlCom = new SqlCommand(selectSql);
                    int num = (int)sqlCom.ExecuteScalar();
                    if (num > 0)
                    {
                        return true;
                    }
                    else
                    {
                        message = "用户名或密码不正确!";
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    return false;
                }
                finally
                {
                    db.Close();
                    
                }
            }
            return false;
        }

        private void picLogin_Click(object sender, EventArgs e)
        {
            string message = "";
            bool isValidateUser = false;
            
            if (ValidateInput())
            {
                isValidateUser = ValidateUser(txtId.Text, txtPwd.Text, cboType.Text, ref message);   
                if (isValidateUser)
                {
                    
                    UserHelper.longinId = txtId.Text;
                    UserHelper.longinType = cboType.Text;
                    StudentForm stform = new StudentForm();
                    stform.Show();
                    this.Visible = false;
                }
                else
                {
                    MessageBox.Show("登陆失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void picExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

运行不报错无异常但是 不管我怎么修改 结果总是执行 不晓得那里的问题 (数据库里有数据)
                else
                {
                    MessageBox.Show("登陆失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
搜索更多相关主题的帖子: 代码 
2008-09-17 14:08
thanklife
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-9-17
收藏
得分:0 
DBHelper类的代码


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace MySchool
{
    public class DBHelper
    {
        public SqlConnection connection;
        public SqlCommand command;
        public SqlDataReader reader;
        //连接数据库的方法
        public bool GetCon()
        {
            try
            {
                connection = new SqlConnection("Data Source=.;Initial Catalog=MySchool;Integrated Security=True");
                connection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return false;
            }
            return true;
        }
        //修改数据库中的数据(插入、删除、更新)
        public int update(string insertSql)
        {
            int num = 0;
            if (GetCon())
            {
                try
                {
                    command = new SqlCommand(insertSql, connection);
                    num = command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }
            return num;
        }
        //查询数据,得到一张虚拟表
        public SqlDataReader QueryRs(string selectSql)
        {
            reader = null;
            if (GetCon())
            {
                try
                {
                    command = new SqlCommand(selectSql, connection);
                    reader = command.ExecuteReader();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message.ToString());
                }
            }
            return reader;
        }
        public int CheckUser(string selectSql)
        {
            int num = 0;
            if (GetCon())
            {
                try
                {
                    command = new SqlCommand(selectSql, connection);
                    num = (int)command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());

                }
                finally
                {
                    connection.Close();
                }
            }
            return num;
        }
        //关闭数据库
        public void Close()
        {
            try
            {
                reader.Close();
                connection.Close();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}
2008-09-17 14:09
快速回复:[请教高手]帮我看看代码那里有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029430 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved