我是这个开发软件世界的新手。所以这可能听起来很傻。
我正在大学开发一个简单的登录系统,我想加快速度,我的教授可能有点混乱,它会危及我们学习一百万件事的时间。关于编程。
如果用户名和密码有效,我可以让程序运行并只打开主表单。
在我研究并制作了一个可在我的数据库中写入数据的注册按钮之前,消息框工作正常。
但是现在,我似乎无法让消息框显示,如果是,成功登录或不是。
我的代码如下登录按钮:
private void VLogin_Click(object sender, EventArgs e)
{
try
{
string ConnectionString = "User ID=*****;Password=*******;" +
"Database=C:\\Users\\marqu_000\\Documents\\WindowsFormsApplication13\\Sistema.GDB;" +
"DataSource=localhost";
FbConnection addDetailsConnection = new FbConnection(ConnectionString);
addDetailsConnection.Open();
string SQLCommandText = "select * from LOGIN where USERNAME=@username and PASSWORD=@password";
FbCommand addDetailsCommand = new FbCommand(SQLCommandText, addDetailsConnection);
addDetailsCommand.Parameters.Add("@username", FbDbType.VarChar, 15).Value = userName.Text;
addDetailsCommand.Parameters.Add("@password", FbDbType.VarChar, 15).Value = userPassword.Text;
FbDataReader reader = addDetailsCommand.ExecuteReader();
while (reader.Read())
{
this.Visible = false;
MainWin MWin = new MainWin();
MWin.ShowDialog();
Application.Exit();
}
MessageBox.Show("Login Successful");
}
catch (Exception x)
{
MessageBox.Show("Invalid username or password");
MessageBox.Show(x.Message);
}
}
这是我的注册按钮代码:
private void registerUser_Click(object sender, EventArgs e)
{
try
{
string ConnectionString = "User ID=****;Password=****;" +
"Database=C:\\Users\\marqu_000\\Documents\\WindowsFormsApplication13\\Sistema.GDB;" +
"DataSource=localhost";
FbConnection addDetailsConnection = new FbConnection(ConnectionString);
addDetailsConnection.Open();
FbCommand SQLCommandTextInsert = new FbCommand("INSERT INTO LOGIN(USERNAME, PASSWORD, LEVEL) VALUES (@username,@password,@level)", addDetailsConnection);
SQLCommandTextInsert.Parameters.AddWithValue("username", registerUsername.Text);
SQLCommandTextInsert.Parameters.AddWithValue("password", registerPassword.Text);
SQLCommandTextInsert.Parameters.AddWithValue("level", registerLevel.Text);
SQLCommandTextInsert.ExecuteNonQuery();
MessageBox.Show("Registration completed!");
}
catch (FbException fbex)
{
//throw fbex;
MessageBox.Show("Invalid information, please verify!");
MessageBox.Show(fbex.Message);
}
}
如果那里有任何无用的线条,以及制作这些东西最简单的方法,我想知道。 我提前感谢你们的帮助。 马可。
答案 0 :(得分:0)
ShowDialog
是一个阻塞调用,这意味着在该调用完成之前不会运行任何代码。在这种情况下,当您的MWin关闭时。在调用ShowDialog或从MWin调用它之前,您需要显示成功消息。
您的登录失败消息仅在发生异常时显示。我想如果读者没有返回任何结果(登录失败),我会正确阅读你的代码,那么你将显示“Login Succesful”,然后不做其他任何事情。
您可以使用if
来确定数据库是否返回结果,登录失败是您的else
条件。
答案 1 :(得分:0)
我终于成功了! 我搜索了如何用IF进行验证并且可以做到。这就是我现在的代码:
try
{
string ConnectionString = "User ID=*****;Password=*******;" +
"Database=C:\\Users\\marqu_000\\Documents\\WindowsFormsApplication13\\Sistema.GDB;" +
"DataSource=localhost";
FbConnection addDetailsConnection = new FbConnection(ConnectionString);
addDetailsConnection.Open();
string SQLCommandText = "select * from LOGIN where USERNAME=@username and PASSWORD=@password";
FbCommand addDetailsCommand = new FbCommand(SQLCommandText, addDetailsConnection);
addDetailsCommand.Parameters.Add("@username", FbDbType.VarChar, 15).Value = userName.Text;
addDetailsCommand.Parameters.Add("@password", FbDbType.VarChar, 15).Value = userPassword.Text;
FbDataReader reader = addDetailsCommand.ExecuteReader();
if (reader.Read())
{
this.Visible = false;
MessageBox.Show("Login Successful!");
MainWin MWin = new MainWin();
MWin.ShowDialog();
}
else
{
MessageBox.Show("Invalid username or password!");
}