Where to put my connection string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Where to put my connection string?

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS 2010
    Posts
    8

    Where to put my connection string?

    I'm working with C# 2010. I placed my connection string into a Web.config file. The references all seem to be working. In trying to pass the connection string to my methods in the DataAccess tier. I've seen some people place their connstring in a static method. I don't know why. I could make it a data member with each class or i could place it in each method. The class level seems better than reduplicating the code in each method. but i don't know why people use a static property.

    Here is my code as I'm currently doing it. I've condensed the code of course. (I do plan to remove the connectionstring from the method's parameter list. If i keep it the way it currently is.)

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using MWLib;
    using System.Configuration;
    
    namespace DataAccess
    {
        public class daStore
        {
            string connStr = ConfigurationManager.ConnectionStrings["myEHRConnectionString"].ConnectionString;
    
            #region " Public properties "
            public static bool pTransactionSuccessful;
            public bool TransactionSuccessful()
            {
                return pTransactionSuccessful;
            }
            public static string pErrorMessage;
            public string ErrorMessage()
            {
                return pErrorMessage;
            }
            public static int pErrorNumber;
            public int ErrorNumber()
            {
                return pErrorNumber;
            }
            public static int pErrorClass;
            public int ErrorClass()
            {
                return pErrorClass;
            }
            public static int pErrorState;
            public int ErrorState()
            {
                return pErrorState;
            }
            public static int pErrorLineNumber;
            public int ErrorLineNumber()
            {
                return pErrorLineNumber;
            }
            public static bool pIsFound;
            public bool IsFound()
            {
                return pIsFound;
            }
            #endregion
    
            #region " Read methods "
            public DataTable GetETLClientList(string ConnectionString)
            {
    
                // Set up parameters in parameter array 
                SqlParameter[] arParms = new SqlParameter[0];
    
                //arParms[0] = new SqlParameter("@Email", SqlDbType.VarChar);
                //arParms[0].Value = Email;
    
    
                SqlTools SSItools = SqlTools.CreateAndConnectSqlTools(ConnectionString);
                DataTable dtETLClient = new DataTable("ETLClient");
                
                try
                {
                    SqlCommand command = new SqlCommand();
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "GetETLClientList";
    
                    using (SqlDataReader dataReader = SSItools.ExecuteDataReader(command))
                    {
                        // A simple google search will show numerous articles and messageboards explaining that a datareader should not be passed between tiers
                        // While it is not a bad thing, it is not a best practice and could cause problems closing the datareader. 
                        // To resolve this problem, i convert the datareader to a datatable.
                        dtETLClient.Load(dataReader);
                        dataReader.Close();
                    }
                   
                }
     
                catch (SqlException ReadError)
                {
                    pErrorMessage = ReadError.Message.ToString();
                    pErrorNumber = ReadError.Number;
                    pErrorClass = ReadError.Class;
                    pErrorState = ReadError.State;
                    pErrorLineNumber = ReadError.LineNumber;
    
                    pTransactionSuccessful = false;
                }
                finally {
                    SSItools.Disconnect();
                    
                }
                return dtETLClient;
            }
       #endregion
    
        }
    }
    Last edited by maurices5000; April 15th, 2013 at 02:29 PM.

  2. #2
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS 2010
    Posts
    8

    Re: Where to put my connection string?

    Well i asked on a different forum. One guy said don't use a static method. I was also told that this was fine just remove the method parameter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center