Free Jobs
Reference




Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: How to connect to IBM DB2 Database from ASP NET c sharp application

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: dhiraj5079 Post Date: 03 Jan 2012 01:49

Introduction:
How to connect to IBM DB2 Database from ASP NET C# application



Main Content:
 THis can be done by OleDbConnection

  Using System.Data.OleDb

Public Collection<TestObject> GetTestData(sampleInput)

{

  string testQuery = GetTestQuery(sampleInput);

  DataSet testDataSet = new DataSet();

  string connectionString = GetConnStringFromConfig();

   using(OleDbConnection connection = new OleDbConnection(connectionString))

   {

     try

     {

      OleDbParameters[] parameters = null;

      List<OleDbParameters> parametersList = GetParameterList(sampleInput);

      if(parametersList != null && parametersList.Count >0)

        {

            parameters = parametersList.ToArray();

            DB2Helper.FillDataSet(connectionString,CommandType.Text,testQuery,testDataSet,null,parameters);

        }

        else

        {

         DB2Helper.FillDataSet(connectionString,CommandType.Text,testQuery,testDataSet,null,null);

        }

        Datatable testDataTable = testDataSet.Table[0];

     }

     catch(OleDbException ex)

     {

       EnterprizeLogging.Logger.Logerror(ex,ModuleName);

     }



   }

}

public string GetTestQuery(SampleInputType sampleInput)

{

  StringBuilder testQuery = new StringBuilder();

  testQuery.Append(" Select Id,Name,City From sampleTableName ");

  int usedParameterCount = 0;

  if(sampleInput.Id != 0)

  {

    if(usedParameterCount == 0)

    {

     testQuery.Append(" Where Id = ? ");

    }

    else

    {

      testQuery.Append(" and Id = ? ");

    }

    usedParameterCount ;

  }

  if(!string.IsNullOrEmpty(sampleInput.Name))

  {

    if(usedParameterCount == 0)

    {

     testQuery.Append(" Where Name = ? ");

    }

    else

    {

      testQuery.Append(" and Name = ? ");

    }

    usedParameterCount ;

  }

  if(!string.IsNullOrEmpty(sampleInput.City))

  {

    if(usedParameterCount == 0)

    {

     testQuery.Append(" Where City = ? ");

    }

    else

    {

      testQuery.Append(" and City = ? ");

    }

    usedParameterCount ;

  }

public List<OleDbParameters> GetParameterList(SampleInputType sampleInput)

{

  List<OleDbParameters> parameterList = new List<OleDbParameters>();

  if(sampleInput.Id != 0)

  {

    parameterList.Add(DataHelper.CreateParm("@Id",DbType.Int32,ParameterDirection.Input,sampleInput.Id));

  }

  if(!string.IsNullOrEmpty(sampleInput.Name))

  {

    parameterList.Add(DataHelper.CreateParm("@Name",DbType.String,ParameterDirection.Input,sampleInput.Name));



  }

  if(!string.IsNullOrEmpty(sampleInput.City))

  {

    parameterList.Add(DataHelper.CreateParm("@City",DbType.String,ParameterDirection.Input,sampleInput.City));



  }

  return parameterList;

}



Write This function in DataHelper Class :

internal static OleDbParameter CreateParm(string parmName,OleDbType type ,ParameterDirection direction, Object value)

{

  OleDbParameter idParm = new OleDbParameter();

  idParm.ParameterName = parmName;

  idParm.Direction = direction;

  idParm.Type = type;

  if(value == null)

  idParm.Value = DBNull.Value

   else

  idParm.Value = value

  return idParm;

}



 

 

Conclusion:
Thus we can connect to IBM DB2 Database from ASP NET c sharp application using System.Data.OleDb



About Writer:
Hi,I am IT Professional From Mumbai


Writer's Link: http://

Writer's Email:







Comments:
Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)