如何使用多线程使用C#从多个数据库中的同一个表中获取数据

时间:2018-04-10 06:48:17

标签: c# asp.net multithreading

此方法缓慢地从多个数据库中获取路径。我想使用多线程从几个数据库中快速获取路径。 ' CnnStr'是'卖家的连接字符串'。我想创建Web服务,但我在项目中使用示例代码,这减缓了我的网站。

public static List<IdTitle> GetPathes(string srcCity)
{
   List<SellerInfo> sellers = GetSellers();
   List<IdTitle> res = new List<IdTitle>();
   List<string> hash = new List<string>();
   for (int i = 0; i < sellers.Count; i++)
   {
       if (string.IsNullOrEmpty(sellers[i].CnnStr) || !string.IsNullOrEmpty(sellers[i].EUrl))
        continue;
       List<IdTitle> pathes = GetPathes(sellers[i], srcCity);
       if (pathes == null || pathes.Count == 0)
           continue;
       for (int j = 0; j < pathes.Count; j++)
          if (!hash.Contains(pathes[j].Title))
          {
              hash.Add(pathes[j].Title);
              res.Add(pathes[j]);
          }
   }
   return res;
}

public static List<IdTitle> GetPathes(SellerInfo info, string srcCity)
{
    if (info == null)
        return null;
    string qry = @"SELECT Paths.ID, City1.Title + N' -> ' + City2.Title AS Title FROM
                    Paths INNER JOIN Cities AS City1 ON Paths.SrcCityID=City1.ID
                    INNER JOIN Cities AS City2 ON Paths.DestCityID=City2.ID";
    if (!string.IsNullOrEmpty(srcCity))
        qry += " WHERE City1.Title="+DbHelper.QtdStr(srcCity);
    try
    {
       IDataReader dr = DbHelper.Instance.ExecuteReader(info.CnnStr, qry);
       return ObjectBinder.FillCollection<IdTitle>(dr);
    }
    catch (Exception ex)
    {
       AddLog(ex);
    }
    return null;
}

0 个答案:

没有答案
相关问题