如何在C#中并行执行SQL查询?

时间:2017-04-12 02:14:00

标签: c# sql sql-server

我有3个SQL查询运行如下,目前我正在运行它们每个顺序消耗时间,我想探索在C#中并行运行它们的选项。我想知道C#中有没有这样的可能性?它甚至可行吗?

sql1 = select DISTINCT A.Name 
       from masterdata.Areas as A,
            ...................
       and SPB.SoftwareProductBuild = 'build1'

sql1 = select DISTINCT A.Name 
       from masterdata.Areas as A,
        ...................
        and SPB.SoftwareProductBuild = 'build2'

sql1 = select DISTINCT A.Name 
       from masterdata.Areas as A,
        ...................
       and SPB.SoftwareProductBuild = 'build3'

SqlCommand cmd = new SqlCommand(sql1, odsConn);

SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();

if (rdr.HasRows)
{
}

SqlCommand cmd = new SqlCommand(sql2, odsConn);
SqlDataReader rdr = cmd.ExecuteReader();

rdr.Read();

if (rdr.HasRows)
{
}

SqlCommand cmd = new SqlCommand(sql3, odsConn);

SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();

if (rdr.HasRows)
{
}

1 个答案:

答案 0 :(得分:0)

将其作为UNIONWHERE SPB.SoftwareProductBuild IN ('build1', 'build2','build3')执行,并让数据库对其进行排序 更高效地使用连接,网络,脑力和可能的CPU。