Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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;
}
Posted
Comments
sreeyush sudhakaran 10-Sep-15 2:58am    
Have a look at Task Parallel Library

Reference Link : https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx
Member 11972515 10-Sep-15 15:17pm    
Not found the solution :(
sreeyush sudhakaran 11-Sep-15 15:34pm    
u made sql data read parallel in thread?
Member 11972515 11-Sep-15 15:46pm    
Yes, but every consult wait his turn with different order only
sreeyush sudhakaran 12-Sep-15 10:53am    
Have a glance at https://www.youtube.com/watch?v=No7QqSc5cl8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900