DBfunctions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;
6 using System.Data.Sql;
7 using System.Data.SqlClient;
8 using System.Windows.Forms;
9 
10 namespace sage.ew.db
11 {
15  public static class DataExtensions
16  {
23  public static IEnumerable<IEnumerable<DataRow>> Partition(this DataTable dataTable, int partitionSize)
24  {
25  var numRows = Math.Ceiling((double)dataTable.Rows.Count);
26  for (var i = 0; i < numRows / partitionSize; i++)
27  {
28  yield return Partition(dataTable, i * partitionSize, i * partitionSize + partitionSize);
29  }
30  }
31  private static IEnumerable<DataRow> Partition(DataTable dataTable, int index, int endIndex)
32  {
33  for (var i = index; i < endIndex && i < dataTable.Rows.Count; i++)
34  {
35  yield return dataTable.Rows[i];
36  }
37  }
38  }
39 
43  public static class DBfunctions
44  {
45 
49  public static bool _lMostrarMensajes = false;
50 
54  public static string _cMensajeError = string.Empty;
55 
59  static DBfunctions()
60  {
61 
62  }
63 
68  public static int _Numero_Ejercicios
69  {
70  get
71  {
72  string lcSql = string.Empty;
73  DataTable ldtExercicis = new DataTable();
74 
75  if (_nNumEjecicios < 0) //No inicializada
76  {
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"]);
80  }
81 
82  return _nNumEjecicios;
83  }
84  }
85  private static int _nNumEjecicios = -1;
86 
91  public static bool _Multi_Ejercicio
92  {
93  get { return (_Numero_Ejercicios > 1); }
94  }
95 
111  public static DataTable _Crear_DtTable(Object[,] tastruc)
112  {
113  int ln_i;
114  string lc_tipo = string.Empty;
115  DataTable tdtTabla = new DataTable();
116 
117  if (tastruc.Rank != 2)
118  {
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);
121  return tdtTabla;
122  }
123 
124  try
125  {
126  for (ln_i = 0; ln_i < tastruc.GetLength(0); ln_i++)
127  {
128  DataColumn loColumna1 = new DataColumn();
129 
130  lc_tipo = ((string)tastruc[ln_i, 1]).ToUpper().Trim();
131 
132  if (lc_tipo == "BYTE" || lc_tipo == "BOOLEAN" || lc_tipo == "INT" || lc_tipo == "DECIMAL" || lc_tipo == "STRING" || lc_tipo == "DATETIME")
133  {
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;
139 
140  switch (lc_tipo)
141  {
142  case "STRING": // Caracter
143  loColumna1.DataType = Type.GetType("System.String");
144  loColumna1.DefaultValue = "";
145  loColumna1.MaxLength = (int)tastruc[ln_i, 2];
146  break;
147  case "DECIMAL": // Numerico o float
148  loColumna1.DataType = Type.GetType("System.Decimal");
149  loColumna1.DefaultValue = 0.0M;
150  break;
151  case "BYTE": // Byte
152  loColumna1.DataType = Type.GetType("System.Byte");
153  loColumna1.DefaultValue = (byte)0;
154  break;
155  case "INT": // Entero
156  loColumna1.DataType = Type.GetType("System.Int32");
157  loColumna1.DefaultValue = 0;
158  break;
159  case "DATETIME": // Fecha o Datetime
160  loColumna1.DataType = Type.GetType("System.DateTime");
161  //loColumna1.DefaultValue = System.DateTime.Now;
162  loColumna1.DefaultValue = null;
163  break;
164  case "BOOLEAN": // Logico
165  loColumna1.DataType = Type.GetType("System.Boolean");
166  loColumna1.DefaultValue = false;
167  break;
168  }
169  tdtTabla.Columns.Add(loColumna1);
170  }
171  else
172  {
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);
176  }
177  } // endfor
178  } // endtry
179  catch (Exception e)
180  {
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);
184  }
185 
186  return tdtTabla;
187  }
188 
194  public static DataTable _Crear_DtTable_Nulls(Object[,] tastruc)
195  {
196  int ln_i;
197  string lc_tipo = string.Empty;
198  DataTable tdtTabla = new DataTable();
199 
200  if (tastruc.Rank != 2)
201  {
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);
204  return tdtTabla;
205  }
206 
207  try
208  {
209  for (ln_i = 0; ln_i < tastruc.GetLength(0); ln_i++)
210  {
211  DataColumn loColumna1 = new DataColumn();
212 
213  lc_tipo = ((string)tastruc[ln_i, 1]).ToUpper().Trim();
214 
215  if (lc_tipo == "BYTE" || lc_tipo == "BOOLEAN" || lc_tipo == "INT" || lc_tipo == "DECIMAL" || lc_tipo == "STRING" || lc_tipo == "DATETIME")
216  {
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;
222 
223  switch (lc_tipo)
224  {
225  case "STRING": // Caracter
226  loColumna1.DataType = Type.GetType("System.String");
227  loColumna1.DefaultValue = "";
228  loColumna1.MaxLength = (int)tastruc[ln_i, 2];
229  break;
230  case "DECIMAL": // Numerico o float
231  loColumna1.DataType = Type.GetType("System.Decimal");
232  loColumna1.DefaultValue = null;
233  break;
234  case "BYTE": // Byte
235  loColumna1.DataType = Type.GetType("System.Byte");
236  loColumna1.DefaultValue = null;
237  break;
238  case "INT": // Entero
239  loColumna1.DataType = Type.GetType("System.Int32");
240  loColumna1.DefaultValue = null;
241  break;
242  case "DATETIME": // Fecha o Datetime
243  loColumna1.DataType = Type.GetType("System.DateTime");
244  loColumna1.DefaultValue = null;
245  break;
246  case "BOOLEAN": // Logico
247  loColumna1.DataType = Type.GetType("System.Boolean");
248  loColumna1.DefaultValue = false;
249  break;
250  }
251  tdtTabla.Columns.Add(loColumna1);
252  }
253  else
254  {
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);
258  }
259  } // endfor
260  } // endtry
261  catch (Exception e)
262  {
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);
266  }
267 
268  return tdtTabla;
269  }
270 
271 
279  public static bool SQLUnionDatatable(ref DataTable tdtPrimero, DataTable tdtSegundo)
280  {
281  if (!SQLCompareDatatable(tdtPrimero, tdtSegundo))
282  return false;
283 
284  //mirem les columnes del primer datatable
285  DataColumn[] newcolumns = new DataColumn[tdtPrimero.Columns.Count];
286  for (int i = 0; i < tdtPrimero.Columns.Count; i++)
287  {
288  newcolumns[i] = new DataColumn(tdtPrimero.Columns[i].ColumnName, tdtPrimero.Columns[i].DataType);
289  }
290 
291  //Carreguem les columnes del segon datatable
292  foreach (DataRow row in tdtSegundo.Rows)
293  {
294  tdtPrimero.LoadDataRow(row.ItemArray, true);
295  }
296  tdtPrimero.EndLoadData();
297 
298  return true;
299  }
300 
301  // PARTE 97449
309  public static bool SQLCompareRowsDatatable(DataTable tdtDatos1, DataTable tdtDatos2)
310  {
311  // Primero comparamos el número de registros
312  if (tdtDatos1.Rows.Count != tdtDatos2.Rows.Count)
313  return false;
314 
315  // Ahora comparamos que tengan la misma estructura
316  if (!SQLCompareDatatable(tdtDatos1,tdtDatos2))
317  return false;
318 
319  // Vamos recorriendo los diferentes registros
320  for (int lnRow = 0; lnRow < tdtDatos1.Rows.Count; lnRow++)
321  {
322  for (int lnColumn = 0; lnColumn < tdtDatos1.Columns.Count; lnColumn++)
323  {
324  if (!Equals(tdtDatos1.Rows[lnRow][lnColumn], tdtDatos2.Rows[lnRow][lnColumn]))
325  return false;
326  }
327  }
328  return true;
329  }
330  // FPARTE 97449
331 
339  public static bool SQLCompareDatatable(DataTable tabla1, DataTable tabla2)
340  {
341  bool llok;
342  llok = false;
343 
344  //si tenen diferent numero de columnes ja son diferents.
345  if (tabla1.Columns.Count != tabla2.Columns.Count)
346  return llok;
347 
348  foreach (DataColumn Col1 in tabla1.Columns)
349  {
350  llok = false;
351 
352  foreach (DataColumn Col2 in tabla2.Columns)
353  {
354  //si es diuen igual
355  if (Col1.Caption == Col2.Caption)
356  {
357  //mirem si ocupen el mateix lloc i son del mateix tipus
358  if (Col1.Ordinal == Col2.Ordinal && Col1.GetType() == Col2.GetType())
359  llok = true;
360  else
361  llok = false;
362  break;
363  }
364  }
365 
366  if (llok == false)
367  break;
368  }
369 
370  return llok;
371  }
372 
373  }
374 
375 }