并行SqlDataReader和reader.Read()慢 [英] Parallel SqlDataReader and reader.Read() Slow

查看:103
本文介绍了并行SqlDataReader和reader.Read()慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在并行模式下进行咨询,或者更好的方式来咨询SQL。



咨询花费3分钟做7个周期来自7只股票,但我需要花更少的时间,也许同时做7次咨询并返回结果。



我的代码问题是reader.Read()maybie在咨询之间震惊。



I want to do a consult in Parallel mode, or which is the better way to do the consult to SQL.

The consult spends 3 minutes doing 7 cycles from 7 stocks, but I need that spend less time, maybe by doing the 7 consults at the same time and return the result.

My problem with the code is that reader.Read() maybie shock between the consults.

List<List<clsExistenciaA>> ListaExistencia = new List<List<clsExistenciaA>>();
SqlConnection objConexion = new SqlConnection();//conexion a sql
try
{
    objConexion.ConnectionString = @"Database=DataDB;Data Source=local;User Id=sa;Password=20011$;MultipleActiveResultSets=True";
    objConexion.Open();
}
catch { MessageBox.Show("No se puede conectar al servidor"); }

try
{
    string almacen;
    int i = 0;
    foreach(clsExistenciaA A in AlmaceneS)
    {
         //Parallel.ForEach(AlmaceneS, clsExistenciaA =>
         //{
         //    lock (AlmaceneS)
         //    {
         //        almacen = clsExistenciaA.CodigoAlm;
         almacen = A.CodigoAlm;

         SqlCommand comm = new SqlCommand();//crea command
         comm.Connection = objConexion;// se agrega la conexion al comando
         comm.CommandType = CommandType.Text;// se define el tipo de comando
         string sql = "";

         sql = "select c_codigo_alm, v_nombre_alm from invalmacen (nolock) where c_pedido_alm='almacen'";
         comm.CommandTimeout = 1000;
         comm.CommandText = sql;// se agrega la consulta a el comando
         SqlDataReader reader = comm.ExecuteReader();// se ejecuta el comando en un data reader

         ListaExistencia.Add(new List<clsExistenciaA>());
         string piezaN = "0";
         string kiloN = "0";

         while(reader.Read())
         {
             if (reader["pieza"].ToString() != "")
             {
                piezaN = reader["pieza"].ToString();
             }
             if (reader["kilos"].ToString() != "")
             {
                 kiloN = reader["kilos"].ToString();
             }

             ListaExistencia[i].Add(new clsExistenciaA()
             {
                 pro = reader["pro"].ToString(),
                 nompro = reader["v_nombre_pro"].ToString(),
                 pieza = Math.Round(double.Parse(piezaN), 2),
                 kilos = Math.Round(double.Parse(kiloN), 2),
                 CodigoAlm = almacen,
              }); // se agregan los datos a un Ilist

          }

          reader.Close();

     }
     ////}
     //});
    return ListaExistencia;
}

推荐答案

; MultipleActiveResultSets = True;
objConexion.Open();
}
catch {MessageBox.Show( No se puede conectar al servidor);}

try
{
< span class =code-keyword> string almacen;
int i = 0 ;
foreach (clsExistenciaA in AlmaceneS)
{
// Parallel.ForEach(AlmaceneS,clsExistenciaA =>
// {
// lock(AlmaceneS)
// {
// almacen = clsExistenciaA.CodigoAlm;
almacen = A.CodigoAlm;

SqlCommand comm = new SqlCommand(); // crea命令
comm.Connection = objConexion; // se agrega la conexion al comando
comm.CommandType = CommandType.Text; // se define el tipo de comando
string sql = ;

sql = 从invalmacen(nolock)中选择c_codigo_alm,v_nombre_alm,其中c_pedido_alm ='almacen' ;
comm.CommandTimeout = 1000 ;
comm.CommandText = sql; // se agrega la consulta a el comando
SqlDataReader reader = comm.ExecuteReader(); // se ejecuta el comando en un data reader

ListaExistencia.Add( new List< clsExistenciaA>());
string piezaN = 0;
string kiloN = 0;

while (reader.Read())
{
if (reader [ pieza]。ToString()!=
{
piezaN = reader [ pieza]。ToString();
}
if (reader [ kilos]。ToString()!=
{
kiloN = reader [ kilos]。ToString();
}

ListaExistencia [i] .Add( new clsExistenciaA()
{
pro = reader [ pro]。ToString(),
nompro = reader [ v_nombre_pro]。ToString(),
pieza = Math.Round( double .Parse(piezaN), 2 ),
kilos = Math.Round( double .Parse(kiloN), 2 ),
CodigoAlm = almacen,
}); // se agregan los datos a un Ilist

}

reader.Close();

}
/// /}
// });
return ListaExistencia;
}
;MultipleActiveResultSets=True"; objConexion.Open(); } catch { MessageBox.Show("No se puede conectar al servidor"); } try { string almacen; int i = 0; foreach(clsExistenciaA A in AlmaceneS) { //Parallel.ForEach(AlmaceneS, clsExistenciaA => //{ // lock (AlmaceneS) // { // almacen = clsExistenciaA.CodigoAlm; almacen = A.CodigoAlm; SqlCommand comm = new SqlCommand();//crea command comm.Connection = objConexion;// se agrega la conexion al comando comm.CommandType = CommandType.Text;// se define el tipo de comando string sql = ""; sql = "select c_codigo_alm, v_nombre_alm from invalmacen (nolock) where c_pedido_alm='almacen'"; comm.CommandTimeout = 1000; comm.CommandText = sql;// se agrega la consulta a el comando SqlDataReader reader = comm.ExecuteReader();// se ejecuta el comando en un data reader ListaExistencia.Add(new List<clsExistenciaA>()); string piezaN = "0"; string kiloN = "0"; while(reader.Read()) { if (reader["pieza"].ToString() != "") { piezaN = reader["pieza"].ToString(); } if (reader["kilos"].ToString() != "") { kiloN = reader["kilos"].ToString(); } ListaExistencia[i].Add(new clsExistenciaA() { pro = reader["pro"].ToString(), nompro = reader["v_nombre_pro"].ToString(), pieza = Math.Round(double.Parse(piezaN), 2), kilos = Math.Round(double.Parse(kiloN), 2), CodigoAlm = almacen, }); // se agregan los datos a un Ilist } reader.Close(); } ////} //}); return ListaExistencia; }


这篇关于并行SqlDataReader和reader.Read()慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆