April 15th, 2013, 02:32 PM
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.)
public class daStore
string connStr = ConfigurationManager.ConnectionStrings["myEHRConnectionString"].ConnectionString;
#region " Public properties "
public static bool pTransactionSuccessful;
public bool TransactionSuccessful()
public static string pErrorMessage;
public string ErrorMessage()
public static int pErrorNumber;
public int ErrorNumber()
public static int pErrorClass;
public int ErrorClass()
public static int pErrorState;
public int ErrorState()
public static int pErrorLineNumber;
public int ErrorLineNumber()
public static bool pIsFound;
public bool IsFound()
#region " Read methods "
public DataTable GetETLClientList(string ConnectionString)
// Set up parameters in parameter array
SqlParameter arParms = new SqlParameter;
//arParms = new SqlParameter("@Email", SqlDbType.VarChar);
//arParms.Value = Email;
SqlTools SSItools = SqlTools.CreateAndConnectSqlTools(ConnectionString);
DataTable dtETLClient = new DataTable("ETLClient");
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.
catch (SqlException ReadError)
pErrorMessage = ReadError.Message.ToString();
pErrorNumber = ReadError.Number;
pErrorClass = ReadError.Class;
pErrorState = ReadError.State;
pErrorLineNumber = ReadError.LineNumber;
pTransactionSuccessful = false;
Last edited by maurices5000; April 15th, 2013 at 03:29 PM.
April 15th, 2013, 04:13 PM
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.
Click Here to Expand Forum to Full Width