Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need to know, I have 2 databases and I need to know how i can set up/define 2 database connections using database factory and DataWorker dataWorker in a 2 saperate class, I already define 2 connections string in web.config file and I already using this for one connection to Db, when i am trying to define for second Db in another class... i am getting error
Posted
Updated 7-Jul-12 9:19am
v2
Comments
[no name] 7-Jul-12 15:44pm    
You might want to read the question that you wrote and ask yourself if you could answer it if you know nothing about your project...

"i am getting error"... means what exactly? Out of the thousands of errors that it could possibly be are we supposed to guess?
ShoaibS 7-Jul-12 15:58pm    
Actually I am getting connection error with second connection string in web.config
ShoaibS 7-Jul-12 16:00pm    
error like this :
Line 18: DataWorker dataWorker2 = new DataWorker(Database.PISConnection);
[no name] 7-Jul-12 16:19pm    
That is a line of code. It is not an error.
[no name] 7-Jul-12 16:21pm    
Okay so? Fix your connection string. Or fix the code that reads the connection string in your web.config file. Or fix the server that the connection string points to.

1 solution

XML
public class DataHelper
   {
       /// <summary>
       /// This method is created for the insert update table using  Stored procedure.
       /// </summary>
       /// <param name="strProcedureName"></param>
       /// <param name="objDbTransaction"></param>
       /// <param name="objDbParameterCollection"></param>
       public static void ExecuteNonQuery(string strProcedureName, DbTransaction objDbTransaction, DbParameter[] objDbParameter)
       {
           DbProviderFactory objDbProviderFactory = DbProviderFactories.GetFactory(Utility.ProviderName);

           DbCommand objDbCommand = objDbProviderFactory.CreateCommand();
           objDbCommand.CommandType = CommandType.StoredProcedure;
           objDbCommand.CommandText = strProcedureName;
           objDbCommand.Connection = objDbTransaction.Connection;
           objDbCommand.Transaction = objDbTransaction;

           foreach (DbParameter objDbParameterNew in objDbParameter)
           {
               objDbCommand.Parameters.Add(objDbParameterNew);
           }


           objDbCommand.ExecuteNonQuery();


       }

       /// <summary>
       /// This method  is created for executing reader using query(Select query/(View)).
       /// </summary>
       /// <param name="strQuery"></param>
       /// <returns></returns>
       public static DbDataReader ExecuteReader(string strQuery, DbParameter[] objDbParameter)
       {

           DbConnection objDbConnection = Utility.GetConnection;
           try
           {
               DbProviderFactory objDbProviderFactory = Utility.GetProviderFactory;

               objDbConnection.Open();

               DbCommand objDbCommand = objDbProviderFactory.CreateCommand();
               objDbCommand.CommandType = CommandType.Text;
               objDbCommand.Connection = objDbConnection;
               objDbCommand.CommandText = strQuery;

               if (objDbParameter != null)
               {
                   foreach (DbParameter objDbParameterNew in objDbParameter)
                   {
                       objDbCommand.Parameters.Add(objDbParameterNew);
                   }
               }
               return objDbCommand.ExecuteReader(CommandBehavior.CloseConnection);
           }
           finally
           {
               objDbConnection.Close();
           }
       }

       /// <summary>
       /// This method is created for select the single value
       /// </summary>
       /// <param name="strQuery"></param>
       /// <returns></returns>
       public static object ExecuteScalar(string strQuery, DbParameter[] objDbParameter)
       {
           DbConnection objDbConnection = Utility.GetConnection;
           try
           {
               DbProviderFactory objDbProviderFactory = Utility.GetProviderFactory;

               objDbConnection.Open();

               DbCommand objDbCommand = objDbProviderFactory.CreateCommand();
               objDbCommand.CommandType = CommandType.Text;
               objDbCommand.CommandText = BuildQuery(strQuery);
               objDbCommand.Connection = objDbConnection;

               if (objDbParameter != null)
               {
                   foreach (DbParameter objDbParameterNew in objDbParameter)
                   {
                       objDbCommand.Parameters.Add(objDbParameterNew);
                   }
               }

               return objDbCommand.ExecuteScalar();
           }

           finally
           {
               objDbConnection.Close();
           }
       }

       /// <summary>
       /// This method returns datatable.
       /// </summary>
       /// <param name="strQuery"></param>
       /// <returns></returns>
       public static DataTable ExecuteDataTable(string strQuery, DbParameter[] objDbParameter)
       {
           DbProviderFactory objDbProviderFactory = Utility.GetProviderFactory;
           DbConnection objDbConnection = Utility.GetConnection;
           objDbConnection.Open();

           DbCommand objDbCommand = objDbProviderFactory.CreateCommand();
           objDbCommand.CommandType = CommandType.Text;
           objDbCommand.CommandText = BuildQuery(strQuery);
           objDbCommand.Connection = objDbConnection;

           if (objDbParameter != null)
           {
               foreach (DbParameter objDbParameterNew in objDbParameter)
               {
                   objDbCommand.Parameters.Add(objDbParameterNew);
               }
           }

           DbDataAdapter objDbDataAdapter = objDbProviderFactory.CreateDataAdapter();
           objDbDataAdapter.SelectCommand = objDbCommand;
           DataTable objDataTable = new DataTable();
           objDbDataAdapter.Fill(objDataTable);
           objDbConnection.Close();
           return objDataTable;
       }

       /// <summary>
       /// This method returns dataset.
       /// </summary>
       /// <param name="strQuery"></param>
       /// <returns></returns>
       public static DataSet ExecuteDataSet(string strQuery)
       {
           DbProviderFactory objDbProviderFactory = Utility.GetProviderFactory;
           DbConnection objDbConnection = Utility.GetConnection;
           objDbConnection.Open();

           DbCommand objDbCommand = objDbProviderFactory.CreateCommand();
           objDbCommand.CommandType = CommandType.Text;
           objDbCommand.CommandText = BuildQuery(strQuery);
           objDbCommand.Connection = objDbConnection;

           DbDataAdapter objDbDataAdapter = objDbProviderFactory.CreateDataAdapter();
           objDbDataAdapter.SelectCommand = objDbCommand;

           DataSet objDataSet = new DataSet();
           objDbDataAdapter.Fill(objDataSet);
           objDbConnection.Close();
           return objDataSet;
       }

       private static string BuildQuery(string strQuery)
       {
           if (Utility.GetProviderFactory.GetType().ToString() == "System.Data.SqlClient.SqlClientFactory")
           {
               strQuery = strQuery.Replace("@", "@");
           }
           else if (Utility.GetProviderFactory.GetType().ToString() == "System.Data.OracleClient.OracleClientFactory")
           {
               strQuery = strQuery.Replace("@", ":");
           }
           else
           {
               strQuery = strQuery.Replace("@", "?");
           }
           return strQuery;
       }
   }
 
Share this answer
 
Comments
[no name] 19-Jul-12 11:01am    
How exactly does this answer the question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900