因此,我有一个名为“ User”的类,其中具有以下方法和代码:
public partial class LoginWindow:Window
{
User u = new User();
private void BtnSignup_Click(object sender, RoutedEventArgs e)
{
u.Login();
}
}
在我的LoginWindow中,我有:
<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT1.500000S" type="static" mediaPresentationDuration="PT0H0M7.53S" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011, http://dashif.org/guildelines/dash264">
<Period duration="PT0H0M7.53S">
<AdaptationSet segmentAlignment="true" maxWidth="1920" maxHeight="1080" maxFrameRate="11988/400" par="16:9" lang="und">
<Representation id="1" mimeType="video/mp4" codecs="avc1.640028" width="1920" height="1080" frameRate="11988/400" sar="1:1" startWithSAP="1" bandwidth="244587">
<BaseURL>1080p_00-intro_track1_dashinit.mp4</BaseURL>
<SegmentBase indexRangeExact="true" indexRange="906-949">
<Initialization range="0-905"/>
</SegmentBase>
</Representation>
<Representation id="3" mimeType="video/mp4" codecs="avc1.64001f" width="1280" height="720" frameRate="11988/400" sar="1:1" startWithSAP="1" bandwidth="155529">
<BaseURL>720p_00-intro_track1_dashinit.mp4</BaseURL>
<SegmentBase indexRangeExact="true" indexRange="904-947">
<Initialization range="0-903"/>
</SegmentBase>
</Representation>
...
</AdaptationSet>
<AdaptationSet>
...
</AdaptationSet>
</Period>
</MPD>
当我尝试通过类实例化调用我的Login方法时,什么都不起作用,为什么呢?我说错了吗?
答案 0 :(得分:1)
这应该可行,尽管我对应该解决的问题发表了评论。
User
类:
public bool Login(SqlConnection con, string email, string password)
{
const string query = "SELECT 1 FROM UsersTBL WHERE Email = @email AND UserPassword = @password";
if (!string.IsNullOrWhiteSpace(email) && !string.IsNullOrWhiteSpace(password))
{
try
{
con.Open();
var cmd = con.CreateCommand();
cmd.CommandText = query;
//Correct SqlDbTypes if necessary
cmd.Parameters.Add("@email", SqlDbType.VarChar);
cmd.Parameters["@email"].Value = email;
cmd.Parameters.Add("@password", SqlDbType.VarChar);
//Should NOT be storing passwords as plain text in the database
cmd.Parameters["@password"].Value = password;
if (cmd.ExecuteScalar() == 1)
return true;
}
catch (Exception e)
{
//log e somehow or eliminate this catch block
}
finally
{
//Close the connection if still open
if (con != null && con.State != ConnectionState.Closed)
con.Close();
}
}
return false;
}
LoginWindow
类:
public partial class LoginWindow : Window
{
private void BtnSignup_Click(object sender, RoutedEventArgs e)
{
var u = new User();
if (u.Login(con, tbxEmail.Text, tbxPassword.Text))
{
AppWindow a = new AppWindow();
a.Show();
}
else
lblMissingParameter.Content = "Incorrect Password or Email entered";
}
}
答案 1 :(得分:1)
为了澄清,您遇到了这个问题,因为User类中的tbxEmail和tbxPassword变量与主类中的变量不同。 您应该在类范围内创建两个变量:
public class User {
TextBox tbxEmail; // could be strings
PasswordBox tbxPassword;
public User (TextBox tbxEmail, TextBox tbxPassword) {
this.tbxEmail = tbxEmail;
this.tbxPassword = tbxPassword;
}
}
然后:
User user = new User(tbxEmail,tbxPassword);
user.Login();
或者,创建一个静态方法(静态方法不能使用全局变量,因此必须将所需的所有内容作为方法的参数传递或在其中创建)。
public static void Login (string email, string password){
// code here
}
答案 2 :(得分:0)
我为我的一个学校项目写了一个基本的登录页面,类似于以下内容:
private void signInButton_Click(object sender, EventArgs e)
{
DataProcedures data = new DataProcedures();
User userInfo = new User(usernameTextbox.Text, passwordTextbox.Text);
userInfo.userId = data.verifyUser(userInfo);
if (userInfo.userId != -1)
{
AppWindow a = new AppWindow();
a.Show();
}
else
{
errorLabel.Show();
}
}
public int verifyUser(User userInfo)
{
MySqlConnection conn = new MySqlConnection(connectionString);
int userId = -1;
string returnedUserName;
string returnedPassword;
try
{
conn.Open();
MySqlCommand checkUserNameCmd = conn.CreateCommand();
checkUserNameCmd.CommandText = "SELECT EXISTS(SELECT userName FROM user WHERE userName = @username)";
checkUserNameCmd.Parameters.AddWithValue("@username", userInfo.username);
returnedUserName = checkUserNameCmd.ExecuteScalar().ToString();
MySqlCommand checkPasswordCmd = conn.CreateCommand();
checkPasswordCmd.CommandText = "SELECT EXISTS(SELECT password FROM user WHERE BINARY password = @password AND userName = @username)";//"BINARY" is used for case sensitivity in SQL queries
checkPasswordCmd.Parameters.AddWithValue("@password", userInfo.password);
checkPasswordCmd.Parameters.AddWithValue("@username", userInfo.username);
returnedPassword = checkPasswordCmd.ExecuteScalar().ToString();
if (returnedUserName == "1" && returnedPassword == "1")
{
MySqlCommand returnUserIdCmd = conn.CreateCommand();
returnUserIdCmd.CommandText = "SELECT userId FROM user WHERE BINARY password = @password AND userName = @username";
returnUserIdCmd.Parameters.AddWithValue("@password", userInfo.password);
returnUserIdCmd.Parameters.AddWithValue("@username", userInfo.username);
userId = (int)returnUserIdCmd.ExecuteScalar();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception thrown verifying user: " + ex);
}
finally
{
conn.Close();
}
return userId;
}
希望这会有所帮助。