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.Data.SqlClient;namespace 教师信息系统
{
    public partial class 登陆 : Form
    {
        public 登陆()
        {
            InitializeComponent();
        }
      
        SqlConnection conn = new SqlConnection();
 
        SqlDataReader sqlDR;
        string shenfen;
        private void button1_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Administrator.WIN-1PNN4JG1988\\Desktop\\数据库\\TeacherConnection.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                
            if (radioButton1.Checked == false && radioButton2.Checked == false && radioButton3.Checked == false)
            { MessageBox.Show("请选择登入模式", "提示窗口"); return; }
            if (textBox1.Text == "")
            { MessageBox.Show("帐号为空,请输入", "提示窗口"); return; }
            if (textBox2.Text == "")
            { MessageBox.Show("密码为空,请输入", "提示窗口"); return; }
            try
            {
                if (this.radioButton3.TabStop)
                {
                    shenfen = "ADMIN";
                                    }
                else if (this.radioButton2.TabStop)
                {
                    shenfen = "学生";
                }
                else if (this.radioButton1.TabStop)
                {
                    shenfen = "教师";                }
                conn.Open();
                               SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                if (shenfen == "ADMIN") { cmd.CommandText = "select * from Manager where ManagerID='" + this.textBox1.Text.ToString() + "'and ManagerPassword='" + this.textBox2.Text.ToString() + "'"; }
                if (shenfen == "学生") { cmd.CommandText = "select * from Student where StuNum='" + this.textBox1.Text + "'and StuPassword='" + this.textBox2.Text + "'"; }
                if (shenfen == "教师") { cmd.CommandText = "select * from Teacher where TeacherID='" + this.textBox1.Text + "'and password='" + this.textBox2.Text + "'"; }
                if (sqlDR.Read())
                {
                    if (shenfen == "教师") 
                    {
                        教师主页面 f1 = new 教师主页面();
                        f1.Show();
                        
                    }
                   if (shenfen == "ADMIN") 
                    {
                        管理员主页面 f1 = new 管理员主页面();
                        f1.Show();
                       
                    }
                   if (shenfen == "学生")
                   {
                       学生主页面 f1 = new 学生主页面();
                       f1.Show();
                       
                   }                    
                }
                else
                {
                    MessageBox.Show("您输入的帐号或密码错误");
                    this.textBox1.Focus();
                    this.textBox1.Text = "";
                    this.textBox2.Text = "";
                }
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);            }
            finally
            {
                conn.Close();
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
    }
}这是做的一个登陆界面,运行后,登陆时(账号,密码 都正确),出现标题所示错误。  本人是新手,希望大家指教,不胜感激!

解决方案 »

  1.   

    具体提示错误在哪一行? if (textBox1.Text.Trim() == "")
      { MessageBox.Show("帐号为空,请输入", "提示窗口"); return; }
      if (textBox2.Text.Trim() == "")
      { MessageBox.Show("密码为空,请输入", "提示窗口"); return; }
      

  2.   

    else if (this.radioButton2.TabStop)
      {
      shenfen = "学生";
      }
      else if (this.radioButton1.TabStop)
      {
      shenfen = "教师";
      }
    else
    {
    MessageBox.show("非法用户!";
    return;
    }
      

  3.   

    string shenfen;
    是不是此变量没有初始化引起的.
      

  4.   

    又仔细看了一遍你的代码 
    你的问题在 SqlDataReader
      sqlDR = command.ExecuteReader();  //少了这句
      if (sqlDR.Read())
      {}
      

  5.   

    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader(VS.80).aspx
      

  6.   

    谢谢4楼的,小cat, 问题解决了~   3Q 怎么给你分呢