Advertisement
hiroyukims

C# Connection class

Jun 14th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5.  
  6.  
  7. namespace Pesquisa_boleto.Classes
  8. {
  9.     public class Dados
  10.     {
  11.         public static string erro = "";
  12.         public static DataTable getDados(string sql, string connection = "")
  13.         {
  14.             DataTable dt = new DataTable();
  15.             erro = "";
  16.             try
  17.             {
  18.                 using (SqlConnection con = getConexaoBD(connection))
  19.                 {
  20.                     using (SqlCommand cmd = new SqlCommand(sql, con))
  21.                     {
  22.                         cmd.CommandType = CommandType.Text;
  23.                         //con.Open();
  24.                         //cmd.ExecuteNonQuery();
  25.                         SqlDataAdapter da = new SqlDataAdapter(cmd);
  26.                         da.Fill(dt);
  27.                         return dt;
  28.                     }
  29.                 }
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 erro = Util.Util.GetExceptionMessage(ex);
  34.                 throw ex;
  35.             }
  36.         }
  37.  
  38.         public static void setDados(string sql, string connection = "")
  39.         {
  40.             erro = "";
  41.             try
  42.             {
  43.                 using (SqlConnection con = getConexaoBD(connection))
  44.                 {
  45.                     using (SqlCommand cmd = new SqlCommand(sql, con))
  46.                     {
  47.                         con.Open();
  48.                         //con.ConnectionTimeout = 600;
  49.                         cmd.ExecuteNonQuery();
  50.                         con.Close();
  51.                     }
  52.                 }
  53.             }
  54.             catch (Exception ex)
  55.             {
  56.                 erro = Util.Util.GetExceptionMessage(ex);
  57.                 throw ex;
  58.             }
  59.         }
  60.  
  61.         public static DataTable execProcedure(string procedure, string[] parametros, string connection = "")
  62.         {
  63.             DataTable dt = new DataTable();
  64.             erro = "";
  65.             bool b = false;
  66.             DateTime dtTemp = DateTime.Now;
  67.             try
  68.             {
  69.                 using (SqlConnection con = getConexaoBD(connection))
  70.                 {
  71.                     using (SqlCommand cmd = new SqlCommand(procedure, con))
  72.                     {
  73.                         cmd.CommandType = CommandType.StoredProcedure;
  74.                         cmd.CommandTimeout = 200;
  75.                         con.Open();
  76.                         SqlCommandBuilder.DeriveParameters(cmd);
  77.                         for (int i = 0; i < parametros.Length; i++)
  78.                         {
  79.                             if (cmd.Parameters[i + 1].DbType == DbType.Boolean)
  80.                             {
  81.                                 b = false;
  82.                                 if (bool.TryParse(parametros[i].ToString(), out b))
  83.                                     cmd.Parameters[i + 1].Value = b;
  84.                                 else
  85.                                     throw new Exception("Erro! Parâmetro " + cmd.Parameters[i + 1].ParameterName + " inválido.");
  86.                             }
  87.                             if (cmd.Parameters[i + 1].DbType == DbType.DateTime)
  88.                             {
  89.                                 if (parametros[i].ToString() == "null" || string.IsNullOrWhiteSpace(parametros[i].ToString()))
  90.                                     cmd.Parameters[i + 1].Value = DBNull.Value;
  91.                                 else
  92.                                 {
  93.                                     if (DateTime.TryParse(parametros[i].ToString(), out dtTemp))
  94.                                         cmd.Parameters[i + 1].Value = dtTemp;
  95.                                     else
  96.                                         throw new Exception("Erro! Parâmetro " + cmd.Parameters[i + 1].ParameterName + " inválido.");
  97.                                 }
  98.                             }
  99.                             else
  100.                                 cmd.Parameters[i + 1].Value = parametros[i].ToString();
  101.                         }
  102.                         SqlDataReader dr = cmd.ExecuteReader();
  103.                         dt.Load(dr);
  104.  
  105.                         //while (!dr.IsClosed && dr.Read())
  106.                         //{
  107.                         //    dt.Load(dr);
  108.                         //}
  109.  
  110.                         return dt;
  111.                     }
  112.                 }
  113.             }
  114.             catch (Exception ex)
  115.             {
  116.                 erro = Util.Util.GetExceptionMessage(ex);
  117.                 throw ex;
  118.             }
  119.         }
  120.  
  121.         public static string setDadosRetorno(string sql, string connection = "")
  122.         {
  123.             erro = "";
  124.             try
  125.             {
  126.                 using (SqlConnection con = getConexaoBD(connection))
  127.                 {
  128.                     using (SqlCommand cmd = new SqlCommand(sql, con))
  129.                     {
  130.                         con.Open();
  131.                         string Retorno = cmd.ExecuteScalar().ToString();
  132.                         con.Close();
  133.                         return Retorno;
  134.                     }
  135.                 }
  136.             }
  137.             catch (Exception ex)
  138.             {
  139.                 erro = Util.Util.GetExceptionMessage(ex);
  140.                 //return erro;
  141.                 throw ex;
  142.             }
  143.         }
  144.  
  145.         private static SqlConnection getConexaoBD(string connection)
  146.         {
  147.             if (string.IsNullOrWhiteSpace(connection))
  148.                 connection = "ConnectionStringName"; //conexão padrão
  149.  
  150.             //obtem a string de conexão do Web.Config e retorna uma nova conexao
  151.             string strConexao = ConfigurationManager.ConnectionStrings[connection].ConnectionString;
  152.             return new SqlConnection(strConexao);
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement