我试图让人们使用他们的用户 ID(int)或他们的用户名(字符串)登录。我考虑这样做的方式是创建两个单独的方法,一个接受字符串,一个接受 int。但我无法让它工作,似乎可能有更好的解决方案。
public static string ForgotPassword ( string username )
{
using ( var conn = new SqlConnection( GetConnectionString() ) )
using ( var cmd = conn.CreateCommand() )
{
conn.Open();
cmd.CommandText =
@"SELECT
Password
FROM
Distributor
WHERE
Username = @username";
cmd.Parameters.AddWithValue( "@username", (string)username );
using ( var reader = cmd.ExecuteReader() )
{
if ( !reader.Read() )
{
// no results found
return null;
}
return reader.GetString( reader.GetOrdinal( "Password" ) );
}
}
}
public static string ForgotPassword ( int username )
{
using ( var conn = new SqlConnection( GetConnectionString() ) )
using ( var cmd = conn.CreateCommand() )
{
conn.Open();
cmd.CommandText =
@"SELECT
Password
FROM
Distributor
WHERE
DistributorID= @username";
cmd.Parameters.AddWithValue( "@username", (int)username );
using ( var reader = cmd.ExecuteReader() )
{
if ( !reader.Read() )
{
// no results found
return null;
}
return reader.GetString( reader.GetOrdinal( "Password" ) );
}
}
}
允许他们使用 user_id 或用户名登录的最佳方式是什么?