此代码用于登录用户。最初我对此很满意,但我想稍微清理一下。有问题的行是:
HotelSearchForm hotelsearchForm = new HotelSearchForm();
hotelsearchForm.ShowDialog();
return selectedUser;
问题是return 语句直到表单关闭后才会执行。到目前为止,我还没有遇到任何问题,但它看起来不正确。我从表示层类调用此方法。有什么建议么?
public static Account selectUser(string userName, string password)
{
Account selectedUser = new Account();
if (sConnection.State == ConnectionState.Closed)
sConnection.Open();
OleDbCommand cmd = sConnection.CreateCommand();
OleDbDataReader dbReader = null;
string sql = "SELECT * FROM [Customer] WHERE ([userName]='" + userName + "' AND [Password]='" + password + "')";
cmd.CommandText = sql;
dbReader = cmd.ExecuteReader();
try
{
while (dbReader.Read())
{
if (dbReader.HasRows)
{
selectedUser.custID = (int)dbReader[0];
selectedUser.firstName = (string)dbReader[1];
selectedUser.lastName = (string)dbReader[2];
selectedUser.phoneNum = (string)dbReader[3];
selectedUser.custEmail = (string)dbReader[4];
selectedUser.userName = (string)dbReader[5];
selectedUser.password = (string)dbReader[6];
MessageBox.Show("Login successful!");
HotelSearchForm hotelsearchForm = new HotelSearchForm();
hotelsearchForm.ShowDialog();
return selectedUser;
}
else
{
return null;
}
}
MessageBox.Show("Incorrect login");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return null;
}