看如下代码    OleDbCommand cmd = new OleDbCommand("select * from [Admin] where [名字] = [@名字]",conn);
            cmd.Parameters.Add("名字",textBox_用户名.Text);            conn.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                if (dr["密码"].ToString() == textBox_密码.Text)
                {
                    Form1 form = new Form1();
                     form.Show(); 
                    this.Hide();      
                }
                else
                {
                    MessageBox.Show("密码错误!");
                }
            }
            else
            {
                MessageBox.Show("无此用户!");
            }
            conn.Close();        } 验证成功,但是登录不到主界面,系统自动退出,各位达人请指教

解决方案 »

  1.   

    application.run(new Form1());
    this.close();
      

  2.   

     其实你只对数据库读了一行, while(dr.read()){....}
      

  3.   

    您好像用的是access数据库吧
    application.run(new Form1());
    this.close();
      

  4.   

     假设当前窗体叫Login:OleDbCommand cmd = new OleDbCommand("select * from [Admin] where [名字] = [@名字]",conn);
      cmd.Parameters.Add("名字",textBox_用户名.Text);  conn.Open();
      OleDbDataReader dr = cmd.ExecuteReader();
      if (dr.Read())
      {
      if (dr["密码"].ToString() == textBox_密码.Text)
      {
       this.DialogResult = DialogResult.OK;
      }
      else
      {
      MessageBox.Show("密码错误!");
      }
      }
      else
      {
      MessageBox.Show("无此用户!");
      }
      conn.Close();  } 在你的Form1窗体后台cs代码中增加
    static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Login login = new Login();
                if (login.ShowDialog() == DialogResult.OK)
                {
                    login.Close();
                    Application.Run(new Form1());
                }
            } 
      

  5.   

    登陆窗体登陆按钮事件 private void btnLogin_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(txtName.Text))
                {
                    if (Common.SingletonConnection.CheckLogin("select * from r_expertinfo where userName='" + txtName.Text + "' and password='" + txtPwd.Text + "'"))
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        KryptonMessageBox.Show("用户名或密码不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    KryptonMessageBox.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtName.Focus();
                }
            }
    主窗体登陆事件private void OnFormLoad(object sender, EventArgs e)
            {
                #region //加载
                FrmLogin log = new FrmLogin();
                if (log.ShowDialog() == DialogResult.OK)
                {
                    //你需要执行的各种操作
                }
                else
                {
                    return;
                }
                this.WindowState = FormWindowState.Maximized;
                #endregion
            }
    program.cs文件中[STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmMain());
            }
    你可以走一下单步调试看看到底是哪一步出的问题
      

  6.   

       Form1 form = new Form1();
      form.Show(); 
      this.Hide(); 貌似是运行到了这里 原来的窗体隐藏了,没有退出吧,
    如果是这样那么问题就在
      Form1 form = new Form1();
      form.Show(); 这里
    应该是这里的问题  lz跟踪看看是不是这里的问题。
      

  7.   

    Form1 form = new Form1();
      form.Show()
    这两句话没执行
      

  8.   

    举个例子:
    当打开第二个窗体时,常常不会把第一个窗体关闭。因为当你在Form1中这样写:Form2 frm2 = new Form2 ( );frm2.Show( ); //或者用frm2.ShowDialog( );this.Close();此时若文件Program.cs中为:Application.EnableVisualStyles( );
    Application.SetCompatibleTextRenderingDefault( false );
    Application.Run(new Form1( ) ); //即以Form1为判断整个程序的运行与结束那么当你打开了第二个窗体,然后关闭它时执行前面代码中的this.Close( );Form1被关闭,整个程序也被关闭了。----------------------------------------------------------------------------------------------------------但如果Program.cs中为:Application.EnableVisualStyles( );
    Application.SetCompatibleTextRenderingDefault( false );Form1 frm = new Form1( );frm.Show( );
    Application.Run( ); //此时整个程序会一直运行不受窗体的影响而在Form1中这样写:Form2  frm2 = new Form2( );frm2.Show();//或者用frm2.ShowDialog( );this.Close( );整个程序还是在运行,一直占用内存,只能通过结束进程关闭它了。要想关闭整个程序只要在Form1中的FormClosing 事件中调用Application.Exit( )方法就能关闭整个程序了。那么怎样不关闭Form1又能关闭整个程序呢?我们可以这样:在Form1中不把它关闭,而是把它隐藏了,用this.Hide( );在Form2中的FormClosing事件中再把隐藏的Form1显示出来,用Application.OpenForms[ " 窗体的类名或者索引号 (Form1/0)" ].Show( );1.)如果Program.cs中为:Form1 frm = new Form1( );frm.Show( );
    Application.Run( ); //此时整个程序会一直运行不受窗体的影响时,要想关闭整个程序要在Form1中的FormClosing事件中写上Application.Exit( );2.)如果Program.cs中为:Application.EnableVisualStyles( );
    Application.SetCompatibleTextRenderingDefault( false );
    Application.Run(new Form1( ) ); //即以Form1为判断整个程序的运行与结束时,当窗体Form1关闭时整个程序也就关闭了。不会占用内存了。