public static UserProfile Login(string userName)
{
//GetSqlConnection()主要是获得数据库连接字符串
using (SqlConnection conn = MessageBoardDb.GetSqlConnection())
{
/*
public static SqlCommand GetSqlCommand(string storedProcedure, SqlConnection conn)
{
SqlCommand cmd = new SqlCommand(storedProcedure, conn);
//使用存储过程
cmd.CommandType = CommandType.StoredProcedure;
//连接超时时间,已设定为90秒
cmd.CommandTimeout = MessageBoardDb.CommandTimeout; return cmd;
}
*/
SqlCommand cmd = MessageBoardDb.GetSqlCommand("[SelectPUser]", conn);
//SelectPUser存储过程需要一个参数
cmd.Parameters.Add("@Username", userName);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
                //如果没有找到此登陆用户,抛出一个新的异常
if (dt.Rows.Count == 0)
{
throw new Exception("Username not found!");
} DataRow dr = dt.Rows[0]; long userId = (long)dr["UserId"]; UserProfile userProfile = UserProfileFactory.Create(userId.ToString());
return userProfile;
}
}