2 using System.Collections.Generic;
7 using System.Data.SqlClient;
15 public static class DataExtensions
23 public static IEnumerable<IEnumerable<DataRow>> Partition(
this DataTable dataTable,
int partitionSize)
25 var numRows = Math.Ceiling((
double)dataTable.Rows.Count);
26 for (var i = 0; i < numRows / partitionSize; i++)
28 yield
return Partition(dataTable, i * partitionSize, i * partitionSize + partitionSize);
31 private static IEnumerable<DataRow> Partition(DataTable dataTable,
int index,
int endIndex)
33 for (var i = index; i < endIndex && i < dataTable.Rows.Count; i++)
35 yield
return dataTable.Rows[i];
43 public static class DBfunctions
49 public static bool _lMostrarMensajes =
false;
54 public static string _cMensajeError =
string.Empty;
68 public static int _Numero_Ejercicios
72 string lcSql =
string.Empty;
73 DataTable ldtExercicis =
new DataTable();
75 if (_nNumEjecicios < 0)
77 lcSql =
"SELECT COUNT(*) AS EJERCICIOS FROM " + DB.SQLDatabase(
"COMUNES",
"EJERCICI");
78 DB.SQLExec(lcSql, ref ldtExercicis);
79 if (ldtExercicis != null && ldtExercicis.Rows.Count > 0) _nNumEjecicios = Convert.ToInt32(ldtExercicis.Rows[0][
"EJERCICIOS"]);
82 return _nNumEjecicios;
85 private static int _nNumEjecicios = -1;
91 public static bool _Multi_Ejercicio
93 get {
return (_Numero_Ejercicios > 1); }
111 public static DataTable _Crear_DtTable(Object[,] tastruc)
114 string lc_tipo =
string.Empty;
115 DataTable tdtTabla =
new DataTable();
117 if (tastruc.Rank != 2)
119 if (_lMostrarMensajes)
120 MessageBox.Show(
"La matriz pasada como parámetro a Crar_DtTable no es de dos dimensiones.",
"Mensaje de EW_GLOBAL", MessageBoxButtons.OK, MessageBoxIcon.Error);
126 for (ln_i = 0; ln_i < tastruc.GetLength(0); ln_i++)
128 DataColumn loColumna1 =
new DataColumn();
130 lc_tipo = ((string)tastruc[ln_i, 1]).ToUpper().Trim();
132 if (lc_tipo ==
"BYTE" || lc_tipo ==
"BOOLEAN" || lc_tipo ==
"INT" || lc_tipo ==
"DECIMAL" || lc_tipo ==
"STRING" || lc_tipo ==
"DATETIME")
134 loColumna1.ColumnName = Convert.ToString(tastruc[ln_i, 0]).Trim().ToLower();
135 loColumna1.Caption = Convert.ToString(tastruc[ln_i, 0]).Trim();
136 loColumna1.AllowDBNull = (((string)tastruc[ln_i, 3]).ToUpper().Trim() ==
"Y") ?
true :
false;
137 loColumna1.ReadOnly =
false;
138 loColumna1.Unique =
false;
143 loColumna1.DataType = Type.GetType(
"System.String");
144 loColumna1.DefaultValue =
"";
145 loColumna1.MaxLength = (int)tastruc[ln_i, 2];
148 loColumna1.DataType = Type.GetType(
"System.Decimal");
149 loColumna1.DefaultValue = 0.0M;
152 loColumna1.DataType = Type.GetType(
"System.Byte");
153 loColumna1.DefaultValue = (byte)0;
156 loColumna1.DataType = Type.GetType(
"System.Int32");
157 loColumna1.DefaultValue = 0;
160 loColumna1.DataType = Type.GetType(
"System.DateTime");
162 loColumna1.DefaultValue = null;
165 loColumna1.DataType = Type.GetType(
"System.Boolean");
166 loColumna1.DefaultValue =
false;
169 tdtTabla.Columns.Add(loColumna1);
173 _cMensajeError =
"Error en Crear_DtTable.\n\nSe pasó un tipo de columna incorrecto (lc_tipo=" + lc_tipo +
")";
174 if (_lMostrarMensajes)
175 MessageBox.Show(_cMensajeError,
"Mensaje dE EW_GLOBAL", MessageBoxButtons.OK, MessageBoxIcon.Error);
181 _cMensajeError =
"Error en Crear_DtTable.\n\n" + e.Message;
182 if (_lMostrarMensajes)
183 MessageBox.Show(_cMensajeError,
"Mensaje dE EW_GLOBAL", MessageBoxButtons.OK, MessageBoxIcon.Error);
194 public static DataTable _Crear_DtTable_Nulls(Object[,] tastruc)
197 string lc_tipo =
string.Empty;
198 DataTable tdtTabla =
new DataTable();
200 if (tastruc.Rank != 2)
202 if (_lMostrarMensajes)
203 MessageBox.Show(
"La matriz pasada como parámetro a Crar_DtTable no es de dos dimensiones.",
"Mensaje de EW_GLOBAL", MessageBoxButtons.OK, MessageBoxIcon.Error);
209 for (ln_i = 0; ln_i < tastruc.GetLength(0); ln_i++)
211 DataColumn loColumna1 =
new DataColumn();
213 lc_tipo = ((string)tastruc[ln_i, 1]).ToUpper().Trim();
215 if (lc_tipo ==
"BYTE" || lc_tipo ==
"BOOLEAN" || lc_tipo ==
"INT" || lc_tipo ==
"DECIMAL" || lc_tipo ==
"STRING" || lc_tipo ==
"DATETIME")
217 loColumna1.ColumnName = Convert.ToString(tastruc[ln_i, 0]).Trim().ToLower();
218 loColumna1.Caption = Convert.ToString(tastruc[ln_i, 0]).Trim();
219 loColumna1.AllowDBNull = (((string)tastruc[ln_i, 3]).ToUpper().Trim() ==
"Y") ?
true :
false;
220 loColumna1.ReadOnly =
false;
221 loColumna1.Unique =
false;
226 loColumna1.DataType = Type.GetType(
"System.String");
227 loColumna1.DefaultValue =
"";
228 loColumna1.MaxLength = (int)tastruc[ln_i, 2];
231 loColumna1.DataType = Type.GetType(
"System.Decimal");
232 loColumna1.DefaultValue = null;
235 loColumna1.DataType = Type.GetType(
"System.Byte");
236 loColumna1.DefaultValue = null;
239 loColumna1.DataType = Type.GetType(
"System.Int32");
240 loColumna1.DefaultValue = null;
243 loColumna1.DataType = Type.GetType(
"System.DateTime");
244 loColumna1.DefaultValue = null;
247 loColumna1.DataType = Type.GetType(
"System.Boolean");
248 loColumna1.DefaultValue =
false;
251 tdtTabla.Columns.Add(loColumna1);
255 _cMensajeError =
"Error en Crear_DtTable.\n\nSe pasó un tipo de columna incorrecto (lc_tipo=" + lc_tipo +
")";
256 if (_lMostrarMensajes)
257 MessageBox.Show(_cMensajeError,
"Mensaje dE EW_GLOBAL", MessageBoxButtons.OK, MessageBoxIcon.Error);
263 _cMensajeError =
"Error en Crear_DtTable.\n\n" + e.Message;
264 if (_lMostrarMensajes)
265 MessageBox.Show(_cMensajeError,
"Mensaje dE EW_GLOBAL", MessageBoxButtons.OK, MessageBoxIcon.Error);
279 public static bool SQLUnionDatatable(ref DataTable tdtPrimero, DataTable tdtSegundo)
281 if (!SQLCompareDatatable(tdtPrimero, tdtSegundo))
285 DataColumn[] newcolumns =
new DataColumn[tdtPrimero.Columns.Count];
286 for (
int i = 0; i < tdtPrimero.Columns.Count; i++)
288 newcolumns[i] =
new DataColumn(tdtPrimero.Columns[i].ColumnName, tdtPrimero.Columns[i].DataType);
292 foreach (DataRow row
in tdtSegundo.Rows)
294 tdtPrimero.LoadDataRow(row.ItemArray,
true);
296 tdtPrimero.EndLoadData();
309 public static bool SQLCompareRowsDatatable(DataTable tdtDatos1, DataTable tdtDatos2)
312 if (tdtDatos1.Rows.Count != tdtDatos2.Rows.Count)
316 if (!SQLCompareDatatable(tdtDatos1,tdtDatos2))
320 for (
int lnRow = 0; lnRow < tdtDatos1.Rows.Count; lnRow++)
322 for (
int lnColumn = 0; lnColumn < tdtDatos1.Columns.Count; lnColumn++)
324 if (!Equals(tdtDatos1.Rows[lnRow][lnColumn], tdtDatos2.Rows[lnRow][lnColumn]))
339 public static bool SQLCompareDatatable(DataTable tabla1, DataTable tabla2)
345 if (tabla1.Columns.Count != tabla2.Columns.Count)
348 foreach (DataColumn Col1
in tabla1.Columns)
352 foreach (DataColumn Col2
in tabla2.Columns)
355 if (Col1.Caption == Col2.Caption)
358 if (Col1.Ordinal == Col2.Ordinal && Col1.GetType() == Col2.GetType())