Code: Detail

Especializações:Asp.net, Ajax, C#, Visual Basic.net, Wcf, XML FrameWork .net 1.1, 2.0 e 4.0, OO, Design Patterns Arquitetura SOA, DDD, MVP e MVVM Html, Css, JavaScript Sql Server 2000, 2005 e 2008 Oracle 8i e 9i Asp3, Visual Basic 6 e COM+

C# - (Portuguese)

EnterpriseLibrary para acessar o Db2 9.7

C# acessando o banco de dados DB2 utilizando o Enterprise Library. Desta maneira você pode controlar transações utilizando o TransactionScope sem ter problemas

Last update: 31/07/2014
C#  DB2  C#+DB2   
 
N/A
N/A
N/A
N/A
 

//******************************************************************************************
//
// PRÉ REQUISITO : Instalar o provider .net do DB2 fazendo o download do site da IBM 
//( http://www-947.ibm.com/support/entry/portal/all_download_links/information_management/db2_for_linux,_unix_and_windows?productContext=1690484112 )
//
//
//*******************************************************************************************





//Configurações no App.config
//---------------------------
<configuration>
  <system.data>
    <DbProviderFactories>
      <add name="IBM DB2 .NET Data Provider" invariant="IBM.Data.DB2" description="IBM DB2 Data Provider for .NET Framework 2.0" type="IBM.Data.DB2.DB2Factory, IBM.Data.DB2, Version=9.0.0.2, Culture=neutral, PublicKeyToken=7c307b91aa13d208"/>
    </DbProviderFactories>
  </system.data>  
  <connectionStrings>
    <!--Desenvolvimento-->
    <add name="ConexaoDB2" connectionString="Database=NOMEDOBANCO;Server=IP:PORTA;User ID=UsuarioAdministrador;Password=SENHA;" providerName="IBM.Data.DB2"/>
  </connectionStrings>







//Aplicação C#
//
//Adicionar Referencia dos seguintes componentes NET:
//
//Microsoft.Practices.EnterpriseLibrary.Data - (Enterprise Library Data Access Application Block)
//Microsoft.Practices.EnterpriseLibrary.Common - (Enterprise Library Shared Library)
//Microsoft.Practices.ServiceLocation


    public class EXEMPLODAO : BaseDAO
    {
        public LojaDTO Obter(LojaDTO loja)
        {
            StringBuilder _comandoSql = new StringBuilder();

            try
            {
                _comandoSql.Append("SELECT LOJA, ");
                _comandoSql.Append("        NOME ");
                _comandoSql.Append("FROM configuracao ");
                _comandoSql.Append("WHERE LOJA = @LOJA ");

                Database db = this.GetDatabase();
                LojaDTO _resultado = new LojaDTO();

                using (System.Data.Common.DbCommand cmd = this.ObterDbCommand(db,_comandoSql.ToString()))
                {
                    db.AddParameter(cmd, "@LOJA", DbType.Int32, ParameterDirection.Input, string.Empty, DataRowVersion.Default, loja.Codigo);

                    IDataReader _dr = db.ExecuteReader(cmd);

                    if (_dr.Read() == true)
                    {
                        _resultado.Codigo = Convert.ToInt32(_dr[0]);
                        _resultado.Nome = _dr[1].toString();
                    }

                    return _resultado;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }  










    public abstract class BaseDAO
    {

        protected Database GetDatabase()
        {
            try
            {
                Database db;

                db = EnterpriseLibraryContainer.Current.GetInstance<Database>("ConexaoDB2");

                return db;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        protected System.Data.Common.DbCommand ObterDbCommand(Database db, string query)
        {


            System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(query);

            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOutComandos"]);

            return cmd;


        }

    }

Source:
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.