使用 WPF 创建登录屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22405879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating login screen with WPF
提问by Ronin
I am trying to create login screen with WPF, i have input username and password in SQL data base and connected with C# code. When i am designing in visual studio, i am not getting any error messages and everything seams fine, but when i run application and login screen shows up, i put username and password in to fields but i still get error message that information are incorrect but application still let me trough to next window.Bellow is my code in xaml and c#.
我正在尝试使用 WPF 创建登录屏幕,我在 SQL 数据库中输入了用户名和密码并与 C# 代码连接。当我在 Visual Studio 中进行设计时,我没有收到任何错误消息并且一切都很好应用程序仍然让我进入下一个窗口。Bellow 是我在 xaml 和 c# 中的代码。
private void buttonLogin_Click(object sender, RoutedEventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Denis\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Login where Username='" + textBoxUsername + "' and Password='" + textBoxPassowrd + "'", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count>0)
{
string username = dataSet.Tables[0].Rows[0]["Username"].ToString();
Close();
}
else
{
MessageBox.Show("Invalid username or password!", "Paznja", MessageBoxButton.OK, MessageBoxImage.Error);
}
this.Hide();
MainWindow mn = new MainWindow();
mn.Show();
this.Close();
}
回答by Sascha
The following lines are always executed:
以下几行总是被执行:
this.Hide();
MainWindow mn = new MainWindow();
mn.Show();
this.Close();
Move them into the if statement (that one being executed if the user is found)
将它们移到 if 语句中(如果找到用户则执行该语句)
回答by TheGeekZn
Put
放
this.Hide();
MainWindow mn = new MainWindow();
mn.Show();
this.Close();
insideyour ifstatement. Putting it after the elsewill make it always run..
PS: You may want to use SQL Parametersto prevent some nasty SQL injection.
在你的if陈述中。把它放在后面else会让它总是运行..
PS:你可能想使用SQL 参数来防止一些讨厌的SQL 注入。
Demo:
演示:
private void buttonLogin_Click(object sender, RoutedEventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Denis\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd = new SqlCommand("Select * from Login where Username='@Name' and Password='@Pass'", conn);
cmd.Parameters.Add(new SqlParameter("Name", textBoxUsername);
cmd.Parameters.Add(new SqlParameter("Pass", textBoxPassowrd);
conn.Open();
SqlDataReader rdr= cmd.ExecuteReader();
string username = null;
if (rdr.HasRows)
{
while(rdr.Read())
{
username = rdr["Username"].ToString();
}
conn.Close();
this.Hide();
MainWindow mn = new MainWindow();
mn.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid username or password!", "Paznja", MessageBoxButton.OK, MessageBoxImage.Error);
conn.Close();
}
}
回答by Ramji
this.Hide();
MainWindow mn = new MainWindow();
mn.Show();
this.Close();
inside your if statement. Putting it after the else will make it always run..
在你的 if 语句中。将它放在 else 之后将使其始终运行..

