在应用程序登录窗体成功登录后,显示主窗体时,登录窗体没有关闭!代码如下:
private void btnLogin_Click(object sender, System.EventArgs e)
{
//定义连接字符串和实例化SqlConnection对象
string strConn="Initial Catalog=sale_online;Data Source=localhost;Integrated Security=SSPI";
SqlConnection myConnection=new SqlConnection (strConn);

//使用DataReader对象读取数据库中数据

strUser=txtUsername.Text ;
strPassword=txtPassword.Text;
string strCommand="select * from users where U_name='"+strUser+"' and U_password='"+strPassword+"'";
SqlCommand myCommand=new SqlCommand (strCommand,myConnection);
try
{
myConnection.Open ();
}
catch (Exception Ex)
{
MessageBox.Show (Ex.ToString() ,"错误");
Application.Exit ();
}
SqlDataReader myReader=myCommand.ExecuteReader ();
//如果为记录集的EOF,即用户名和口令不合法
if (!myReader.Read ())
{
MessageBox.Show ("用户名或口令错误!","警告");
txtUsername.Focus ();
}
else
{
myReader.Close ();
myConnection.Close ();
frmMain fMain=new frmMain();
fMain.ShowDialog();
this.Close();
}

解决方案 »

  1.   

    fMain.ShowDialog();
    this.Close();换一下顺序
    this.Close();
    fMain.ShowDialog();
      

  2.   

    看你的代码,是把登录框当成了主窗体,这样是不能把登录框关掉的。你只有用hide把其隐藏。
    最好,你建一个启动模块,来调用所有窗体,
    判断登录是否成功,用一个返回值或一个全局变量来判断。
      

  3.   

    并且你不能用fMain.ShowDialog();应该用fMain.Show();
      

  4.   

    是的,我是想把登录窗口关闭后,打开新的主窗体.如果单独建一个启动模块是可以的,但为什么把登录窗体当启动模块就不行呢?前面几位提到的顺序和SHOW的方法也还是不能解决!
      

  5.   

    TO-tl_pear(飘叶寻梦) 
    谢谢你的解答
    也谢谢大家!
      

  6.   

    你应该这样
       Login login=new Login();
       if(login.showDialog()==DialogResult.OK)
       {
              Application.Run(fMain);
        }
      

  7.   

    fMain.ShowDialog();
    this.Close();换一下顺序
    this.Close();
    fMain.ShowDialog();