Regards , I hope you can help me, I am a student and I take your help please.
I have asked another question but I do not have much action. Interruption Mode
Work with C #.
I have in my MySQL database a table that contains several thousand records with a mediumblob field that contains a pdf, I must extract all the pdf to an address already defined.
To achieve this I am extracting 100 of 100 BD documents, I do it in a backgroundworker but after a while the system fails.
I use the following code that starts when I click on a button:
private void btnRespaldar_Click(object sender, EventArgs e)
{
btnRespaldar.Text = "Espere...";
btnRespaldar.Enabled = false;
pgbDescargarPDF.Visible = true;
lblPorcent.Visible = true;
bgwEscribirDoc.RunWorkerAsync(); //inicia el evento del backgroundWorker
}
// codigo del backgroundWorker
private void bgwEscribirDoc_DoWork(object sender, DoWorkEventArgs e)
{
// variables del evento
int cantidad = 0;
double valPgb = 0;
double prog = 0;
// Obtiene la cantidad de registros que contiene mi tabla en la BD
using (GestorDocumento CDocumento = new GestorDocumento())
{
DataTable miDataTable = new DataTable();
miDataTable = CDocumento.ContarDocumentos();
cantidad = Convert.ToInt16(miDataTable.Rows[0][0].ToString());
}
// valPgb funciona para dar un valor de a sumar al progressBar
valPgb = cantidad / 100;
valPgb = 100 / valPgb;
//
try
{
byte[] documento = null;
// j aumenta de 100 en 100 ya que el procedimiento almacenado que recolecta mis
// documentos pdf capta solo 100 registros.
for (int j = 0; j <= cantidad; j = j + 100)
{
using (GestorDocumento elDocumento = new GestorDocumento())
{
DataTable miDataTable = new DataTable();
//se guardan los 100 resultados en un datatable para despues guardarlos
// en la ruta establesida
miDataTable = elDocumento.CargarDocumentoTodo(j);
int filas = miDataTable.Rows.Count;
if (filas >= 1)
{
// Recorre el datatable y extrae los documentos y los guarda en un
// una ruta x (lblRuta.Text)
for (int i = 1; i <= filas; i++)
{
documento = (byte[])miDataTable.Rows[i - 1][1];
File.WriteAllBytes(lblRuta.Text + miDataTable.Rows[i - 1][0].ToString() + ".pdf", documento);
}
}
miDataTable.Dispose();
}
// prog toma el valor que le sera enviado al value del progressBar
prog = prog+valPgb;
bgwEscribirDoc.ReportProgress((int)prog);
}
}
catch (Exception)
{
frmMensaje frm = new frmMensaje("Ruta de respaldo no definida ", "Precaución", "PRECAUCIÓN");
frm.ShowDialog();
}
}
//indica el progreso del backgroundWorker
private void bgwEscribirDoc_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// progreso del bgw
pgbDescargarPDF.Value = e.ProgressPercentage;
lblPorcent.Text = e.ProgressPercentage + "%";
}
//cuando termina el backgroundWorker
private void bgwEscribirDoc_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// el trabajo del bgw completado
btnRespaldar.Enabled = true;
btnRespaldar.Text = "Respaldar Documentos";
pgbDescargarPDF.Visible = false;
lblPorcent.Visible = false;
frmMensaje frm2 = new frmMensaje("Documentos respaldados", "Confirmación", "CONFIRMACIÓN");
frm2.ShowDialog();
}
Note: I have noticed that the time it takes to extract the first 100 pdf package is less than the time it takes to extract the second pdf package.
The column # Process represents the process of backing up 100 packages, the second column is the number of seconds that the program took to develop that process, the third column represents the difference in seconds between that process and the previous one.