我想讓人們使用他們的用戶名(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或用戶名登錄的最佳方式?
當你最初嘗試時沒有奏效? – 2012-04-17 15:45:47
你用這種方法得到了什麼錯誤? – Tuan 2012-04-17 15:48:17
我從未收到過錯誤。它總是假定我正在調用該方法的字符串版本。我可以用testuser和1234作爲密碼登錄。但是當我做了101和密碼1234它不會在數據庫中找到任何東西。 – 2012-04-17 15:54:00