473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Parallel SqlDataReader and reader.Read()

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.



Expand|Select|Wrap|Line Numbers
  1. List<List<clsExistenciaA>> ListaExistencia = new List<List<clsExistenciaA>>();
  2. SqlConnection objConexion = new SqlConnection();//conexion a sql
  3. try
  4. {
  5.     objConexion.ConnectionString = @"Database=DataDB;Data Source=local;User Id=sa;Password=20011$;MultipleActiveResultSets=True";
  6.     objConexion.Open();
  7. }
  8. catch { MessageBox.Show("No se puede conectar al servidor"); }
  9.  
  10. try
  11. {
  12.     string almacen;
  13.     int i = 0;
  14.     foreach(clsExistenciaA A in AlmaceneS)
  15.     {
  16.          //Parallel.ForEach(AlmaceneS, clsExistenciaA =>
  17.          //{
  18.          //    lock (AlmaceneS)
  19.          //    {
  20.          //        almacen = clsExistenciaA.CodigoAlm;
  21.          almacen = A.CodigoAlm;
  22.  
  23.          SqlCommand comm = new SqlCommand();//crea command
  24.          comm.Connection = objConexion;// se agrega la conexion al comando
  25.          comm.CommandType = CommandType.Text;// se define el tipo de comando
  26.          string sql = "";
  27.  
  28.          sql = "select c_codigo_alm, v_nombre_alm from invalmacen (nolock) where c_pedido_alm='almacen'";
  29.          comm.CommandTimeout = 1000;
  30.          comm.CommandText = sql;// se agrega la consulta a el comando
  31.          SqlDataReader reader = comm.ExecuteReader();// se ejecuta el comando en un data reader
  32.  
  33.          ListaExistencia.Add(new List<clsExistenciaA>());
  34.          string piezaN = "0";
  35.          string kiloN = "0";
  36.  
  37.          while(reader.Read())
  38.          {
  39.              if (reader["pieza"].ToString() != "")
  40.              {
  41.                 piezaN = reader["pieza"].ToString();
  42.              }
  43.              if (reader["kilos"].ToString() != "")
  44.              {
  45.                  kiloN = reader["kilos"].ToString();
  46.              }
  47.  
  48.              ListaExistencia[i].Add(new clsExistenciaA()
  49.              {
  50.                  pro = reader["pro"].ToString(),
  51.                  nompro = reader["v_nombre_pro"].ToString(),
  52.                  pieza = Math.Round(double.Parse(piezaN), 2),
  53.                  kilos = Math.Round(double.Parse(kiloN), 2),
  54.                  CodigoAlm = almacen,
  55.               }); // se agregan los datos a un Ilist
  56.  
  57.           }
  58.  
  59.           reader.Close();
  60.  
  61.      }
  62.      ////}
  63.      //});
  64.     return ListaExistencia;
  65. }
Sep 10 '15 #1
1 5426
Its acepted the Bump?
Sep 10 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ghost | last post by:
Can I get record as DataRow object from SqlDataReader (if yes how)?
2
by: Matthias S. | last post by:
Hi, I've written a simple app which should just fetch some data from a database and render the results into a ListView. In order to not freeze the GUI, I'm using a BackgroundWorker. The...
0
by: Max | last post by:
I'm having some problems using SqlDataReader in the Microsoft.ApplicationBlocks.Data.SqlHelper class. In some cases it seems that the cursor does not move, and I'm not sure what is causing this. ...
4
by: MarkusR | last post by:
If I run the stored proc in the Query Analyzer this works and I get the expected result set back. However when I run it in my application I get a results set of one row but when I try to access the...
2
by: jarod1701 | last post by:
Hi everyone, is there a way to access every record from a SqlDataReader via "foreach" ? I want to write a class which (besides doing some other stuff) retrieves a recordset using...
13
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the...
3
by: Osamede.Zhang | last post by:
I have some code like this: SqlCommand cmd = new SqlCommand("get_storeid_byuser", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value...
3
by: Analizer1 | last post by:
Im looking for example of how to get data into a table without using Table.load(reader) ' using the read() in a loop SqlDataReader reader = Cmd.ExecuteReader(); while (reader.read()) {
7
by: Parul Vinod | last post by:
I am using Visual Studio 2008; I am executing one query and its results are coming in the datareader dr1; I have checked this in debug window. Even though values are coming inside datareader it's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.