我有一个 ASP.NET 方法,它从数据库中提取用户数据并为我正在构建的 Web 应用程序的用户创建登录凭据。
var userDataList = new List<UserInfo>();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
try
{
//open SQL connection
conn.Open();
SqlCommand getSumOfHours = new SqlCommand("SELECT LoginName, Email FROM [User] WHERE Deleted IS NULL", conn);
//execute command and retrieve primary key from the above insert and assign to variable
SqlDataReader reader = getSumOfHours.ExecuteReader();
while (reader.Read())
{
userDataList.Add(new UserInfo
{
userName = reader.GetString(0),
email = reader.GetString(1)
});
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.ToString());
}
finally
{
conn.Close();
}
foreach (var item in userDataList)
{
//System.Diagnostics.Debug.WriteLine(item.userName + " " + item.email);
if (!(Roles.RoleExists("user")))
{
Roles.CreateRole("user");
}
if (Membership.GetUser(item.userName) == null)
{
Membership.CreateUser(item.userName, "password", item.email);
Roles.AddUserToRole(item.userName, "user");
}
}
}
public class UserInfo
{
public string userName { get; set; }
public string email { get; set; }
}
它工作得很好,但目前我通过按下页面上的按钮来执行它。这并不理想,因为有人必须记住按下按钮才能执行。当站点在服务器上运行时,是否有可能让站点每隔几个小时在幕后执行这段代码?