在应用程序登录窗体成功登录后,显示主窗体时,登录窗体没有关闭!代码如下:
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();
}
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();
}
this.Close();换一下顺序
this.Close();
fMain.ShowDialog();
最好,你建一个启动模块,来调用所有窗体,
判断登录是否成功,用一个返回值或一个全局变量来判断。
谢谢你的解答
也谢谢大家!
Login login=new Login();
if(login.showDialog()==DialogResult.OK)
{
Application.Run(fMain);
}
this.Close();换一下顺序
this.Close();
fMain.ShowDialog();