Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- namespace Pesquisa_boleto.Classes
- {
- public class Dados
- {
- public static string erro = "";
- public static DataTable getDados(string sql, string connection = "")
- {
- DataTable dt = new DataTable();
- erro = "";
- try
- {
- using (SqlConnection con = getConexaoBD(connection))
- {
- using (SqlCommand cmd = new SqlCommand(sql, con))
- {
- cmd.CommandType = CommandType.Text;
- //con.Open();
- //cmd.ExecuteNonQuery();
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- da.Fill(dt);
- return dt;
- }
- }
- }
- catch (Exception ex)
- {
- erro = Util.Util.GetExceptionMessage(ex);
- throw ex;
- }
- }
- public static void setDados(string sql, string connection = "")
- {
- erro = "";
- try
- {
- using (SqlConnection con = getConexaoBD(connection))
- {
- using (SqlCommand cmd = new SqlCommand(sql, con))
- {
- con.Open();
- //con.ConnectionTimeout = 600;
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
- catch (Exception ex)
- {
- erro = Util.Util.GetExceptionMessage(ex);
- throw ex;
- }
- }
- public static DataTable execProcedure(string procedure, string[] parametros, string connection = "")
- {
- DataTable dt = new DataTable();
- erro = "";
- bool b = false;
- DateTime dtTemp = DateTime.Now;
- try
- {
- using (SqlConnection con = getConexaoBD(connection))
- {
- using (SqlCommand cmd = new SqlCommand(procedure, con))
- {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandTimeout = 200;
- con.Open();
- SqlCommandBuilder.DeriveParameters(cmd);
- for (int i = 0; i < parametros.Length; i++)
- {
- if (cmd.Parameters[i + 1].DbType == DbType.Boolean)
- {
- b = false;
- if (bool.TryParse(parametros[i].ToString(), out b))
- cmd.Parameters[i + 1].Value = b;
- else
- throw new Exception("Erro! Parâmetro " + cmd.Parameters[i + 1].ParameterName + " inválido.");
- }
- if (cmd.Parameters[i + 1].DbType == DbType.DateTime)
- {
- if (parametros[i].ToString() == "null" || string.IsNullOrWhiteSpace(parametros[i].ToString()))
- cmd.Parameters[i + 1].Value = DBNull.Value;
- else
- {
- if (DateTime.TryParse(parametros[i].ToString(), out dtTemp))
- cmd.Parameters[i + 1].Value = dtTemp;
- else
- throw new Exception("Erro! Parâmetro " + cmd.Parameters[i + 1].ParameterName + " inválido.");
- }
- }
- else
- cmd.Parameters[i + 1].Value = parametros[i].ToString();
- }
- SqlDataReader dr = cmd.ExecuteReader();
- dt.Load(dr);
- //while (!dr.IsClosed && dr.Read())
- //{
- // dt.Load(dr);
- //}
- return dt;
- }
- }
- }
- catch (Exception ex)
- {
- erro = Util.Util.GetExceptionMessage(ex);
- throw ex;
- }
- }
- public static string setDadosRetorno(string sql, string connection = "")
- {
- erro = "";
- try
- {
- using (SqlConnection con = getConexaoBD(connection))
- {
- using (SqlCommand cmd = new SqlCommand(sql, con))
- {
- con.Open();
- string Retorno = cmd.ExecuteScalar().ToString();
- con.Close();
- return Retorno;
- }
- }
- }
- catch (Exception ex)
- {
- erro = Util.Util.GetExceptionMessage(ex);
- //return erro;
- throw ex;
- }
- }
- private static SqlConnection getConexaoBD(string connection)
- {
- if (string.IsNullOrWhiteSpace(connection))
- connection = "ConnectionStringName"; //conexão padrão
- //obtem a string de conexão do Web.Config e retorna uma nova conexao
- string strConexao = ConfigurationManager.ConnectionStrings[connection].ConnectionString;
- return new SqlConnection(strConexao);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement