Advertisement
robhunter

SelectBD.cs

Mar 11th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. namespace Seguros_Sociales
  6. {
  7.     public class SelectBD
  8.     {
  9.         DataSet m_Dataset;
  10.         SqlDataAdapter m_DataAdapter;
  11.         DataRow m_dr;
  12.         private int m_indice;
  13.         private int m_PageSize;
  14.  
  15.         public SelectBD()
  16.         {
  17.             m_Dataset = null;
  18.             m_dr = null;
  19.             m_indice = 0;
  20.             m_PageSize = 0;
  21.         }
  22.  
  23.         public SelectBD(SqlConnection conex, string SQL)
  24.             : this()
  25.         {
  26.             Open(conex, SQL);
  27.         }
  28.  
  29.         public SelectBD(SqlConnection conex, string SQL, string tabla)
  30.             : this()
  31.         {
  32.             Open(conex, SQL, tabla);
  33.         }
  34.  
  35.         ~SelectBD()
  36.         {
  37.             if (m_Dataset != null)
  38.                 m_Dataset.Clear();
  39.             m_Dataset = null;
  40.         }
  41.  
  42.         public int PageSize
  43.         {
  44.             get
  45.             {
  46.                 return m_PageSize;
  47.             }
  48.             set
  49.             {
  50.                 m_PageSize = value;
  51.             }
  52.         }
  53.  
  54.         public bool Open(SqlConnection conex, string SQL)
  55.         {
  56.             return this.Open(conex, SQL, "");
  57.         }
  58.  
  59.         public bool Open(SqlConnection conex, string SQL, string tabla)
  60.         {
  61.             m_Dataset = new DataSet();
  62.             m_DataAdapter = new SqlDataAdapter(SQL, conex);
  63.             SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(m_DataAdapter);
  64.             if (tabla != "") // si llega el nombre de una tabla
  65.                 m_DataAdapter.Fill(m_Dataset, tabla);
  66.             else
  67.                 m_DataAdapter.Fill(m_Dataset);
  68.  
  69.             if (m_Dataset == null)
  70.                 return false;
  71.  
  72.             if (m_Dataset.Tables.Count == 0)
  73.                 m_indice = 0;
  74.             m_indice = 1;
  75.             return true;
  76.         }
  77.  
  78.         public object Campo(string indice)
  79.         {
  80.             return m_Dataset.Tables[0].Rows[m_indice - 1][indice];
  81.         }
  82.  
  83.         public string CampoString(string indice)
  84.         {
  85.             return Campo(indice).Equals(DBNull.Value) ? "" : Campo(indice).ToString();
  86.         }
  87.  
  88.         public DataRow GetCurrentDataRow()
  89.         {
  90.             return m_Dataset.Tables[0].Rows[m_indice - 1];
  91.         }
  92.  
  93.         public object Campo(int indice)
  94.         {
  95.             return m_Dataset.Tables[0].Rows[m_indice - 1][indice];
  96.         }
  97.  
  98.         public string CampoString(int indice)
  99.         {
  100.             return Campo(indice).Equals(DBNull.Value) ? "" : Campo(indice).ToString();
  101.         }
  102.  
  103.         public void NuevoRegistro(string tabla)
  104.         {
  105.             m_dr = m_Dataset.Tables[tabla].NewRow();
  106.         }
  107.  
  108.         public void InsertaRegistro(string tabla)
  109.         {
  110.             m_Dataset.Tables[tabla].Rows.Add(m_dr);
  111.             m_DataAdapter.Update(m_Dataset, tabla);
  112.             m_Dataset.AcceptChanges();
  113.             m_dr = null;
  114.         }
  115.  
  116.         public void SetCampo(string indice, object valor)
  117.         {
  118.             if (m_dr != null)
  119.                 m_dr[indice] = valor;
  120.             else
  121.                 m_Dataset.Tables[0].Rows[m_indice - 1][indice] = valor;
  122.         }
  123.  
  124.         public void SetCampo(int indice, object valor)
  125.         {
  126.             if (m_dr != null)
  127.                 m_dr[indice] = valor;
  128.             else
  129.                 m_Dataset.Tables[0].Rows[m_indice][indice] = valor;
  130.         }
  131.  
  132.         public void SetCampo(int indice, byte[] valor)
  133.         {
  134.             if (m_dr != null)
  135.                 m_dr[indice] = valor;
  136.             else
  137.                 m_Dataset.Tables[0].Rows[m_indice][indice] = valor;
  138.         }
  139.  
  140.         public void AcceptChanges()
  141.         {
  142.             m_DataAdapter.Update(m_Dataset);
  143.         }
  144.  
  145.         public void AcceptChanges(string tabla)
  146.         {
  147.             m_DataAdapter.Update(m_Dataset, tabla);
  148.         }
  149.  
  150.         public void MoveFirst()
  151.         {
  152.             m_indice = 1;
  153.         }
  154.  
  155.         public void MoveLast()
  156.         {
  157.             m_indice = m_Dataset.Tables[0].Rows.Count;
  158.         }
  159.  
  160.         public void MoveNext()
  161.         {
  162.             if (!EOF)
  163.                 m_indice = m_indice + 1;
  164.         }
  165.  
  166.         public bool EOF
  167.         {
  168.             get
  169.             {
  170.                 if (m_Dataset.Tables[0].Rows.Count <= 0)
  171.                     return true;
  172.                 if (m_indice > m_Dataset.Tables[0].Rows.Count)
  173.                     return true;
  174.                 return false;
  175.             }
  176.         }
  177.  
  178.         public int Count
  179.         {
  180.             get
  181.             {
  182.                 return m_Dataset.Tables[0].Rows.Count;
  183.             }
  184.         }
  185.  
  186.         public int PageCount
  187.         {
  188.             get
  189.             {
  190.                 if (m_PageSize > 0)
  191.                     return (Count / m_PageSize) + (Count % m_PageSize > 0 ? 1 : 0);
  192.                 return Count;
  193.             }
  194.         }
  195.  
  196.         public int AbsolutePage
  197.         {
  198.             get
  199.             {
  200.                 if (m_PageSize > 0)
  201.                     return (m_indice / m_PageSize) + 1;
  202.                 return 1;
  203.             }
  204.             set
  205.             {
  206.                 if (value <= 0)
  207.                     m_indice = 0;
  208.                 else if (value > Count)
  209.                     m_indice = (int)Count;
  210.                 else
  211.                 {
  212.                     if (m_PageSize > 0)
  213.                         m_indice = (m_PageSize * (value - 1)) + 1;
  214.                     else
  215.                         m_indice = 1;
  216.                 }
  217.             }
  218.         }
  219.  
  220.         public SqlDataAdapter DataAdapter
  221.         {
  222.             get
  223.             {
  224.                 return m_DataAdapter;
  225.             }
  226.         }
  227.  
  228.         public DataSet DataSet
  229.         {
  230.             get
  231.             {
  232.                 return m_Dataset;
  233.             }
  234.         }
  235.  
  236.         public DataTable DataTable
  237.         {
  238.             get
  239.             {
  240.                 return m_Dataset.Tables[0];
  241.             }
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement