/// <summary>
/// 从数据库中一次性获取用户信息放入哈希缓存中,避免重复的访问数据库
/// </summary>
/// <param name="cellht">手机号哈希</param>
/// <param name="mailht">邮箱哈希</param>
public static void GetUserInfoHash(ref Hashtable cellht, ref Hashtable mailht)
{
try
{
string strSqlCondition = "Select * from UserInfo";
DataTable nDt = Data.GetDataTable(strSqlCondition);
if (nDt != null && nDt.Rows.Count > 0)
{
for (int i = 0; i < nDt.Rows.Count; i++)
{
string loginName = nDt.Rows[i]["LoginName"] as string;
string cellphone = nDt.Rows[i]["Cellphone"] as string;//手机号
string Email = nDt.Rows[i]["Email"] as string;//邮箱
if (!String.IsNullOrEmpty(loginName))
{
cellht.Add(loginName, cellphone);
mailht.Add(loginName, Email);
}
}
}
}
catch (Exception ex)
{
Log.AddLog("SendMsgAndMailToCurrentTask", "GetUserInfoHash Exception:" + ex.Message);
}
}
Hashtable htCellphone = new Hashtable();
Hashtable htEmail = new Hashtable();
GetUserInfoHash(g2, ref htCellphone, ref htEmail);
string AssignTostr = key;//hashkey
if (htCellphone.Contains(AssignTostr))
{
string Cellphone = htCellphone[AssignTostr] as string;
}
若需要重复的通过账户访问数据库获取数据,以实现某些功能场景,为了避免频繁的连接加大服务器的开销,可以将所需要的数据,一次取出在缓存中,然后再进行操作缓存,HashTable只是个简单的例子