adamtrask
asked on
How to save code for data access in a central place
I am a novice C# programmer and need to learn where and how to store the data access code in a central place in a windows applications, developed using Visual Studio 2012.
I know that in ASP.Net I use the configuration file to place and access reference to the code. But how does this apply to a C# Windows application?
What I have been doing for a long time is to write almost the same code or almost the same code for data access over and over:
Something like the method below with little variations (it could be "void" instead of data type):
public DataTable GetMyData()
{
SqlConnection con;
con = new SqlConnection("Server=mySe rver;;Data base=myDat abase;inte grated security=True");
SqlCommand comm = new SqlCommand("Select id,Name from myTable order by LastName", con);
SqlDataAdapter dataAdapter1 = new SqlDataAdapter(comm);
DataSet ds1 = new DataSet();
dataAdapter1.Fill(ds1, "tableNamef");
return ds1.Tables["tablwNamw"];
}
Please give example if possible.
Thanks
Adam
I know that in ASP.Net I use the configuration file to place and access reference to the code. But how does this apply to a C# Windows application?
What I have been doing for a long time is to write almost the same code or almost the same code for data access over and over:
Something like the method below with little variations (it could be "void" instead of data type):
public DataTable GetMyData()
{
SqlConnection con;
con = new SqlConnection("Server=mySe
SqlCommand comm = new SqlCommand("Select id,Name from myTable order by LastName", con);
SqlDataAdapter dataAdapter1 = new SqlDataAdapter(comm);
DataSet ds1 = new DataSet();
dataAdapter1.Fill(ds1, "tableNamef");
return ds1.Tables["tablwNamw"];
}
Please give example if possible.
Thanks
Adam
You can have it in the Busines Logic layer, or a generic function that'll take the sql as string parameter. And reuse the connection object as much as possible. What is the purpose you are trying to solve here exactly?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Connectonstrings should e stored in config file or in strings in the Resources. Check it out.
ASKER
Thank you both...
Yes, the main purpose is to avoid hard coding the code. I used it when I was experimenting with ASP.Net and learned to store the connection in a configuration file which was automatically created in each project. It looks like I need to learn to create one myself in C#.
I intend to use the example referred by expert Anarki but I need to check if i can use SqlServer instead of MS.Access. This is the plan for the weekend and I will see if I can figure it out.
P.S. Thank your for the try-catch remark anarki_jimbel. Yes, I will use it.
Yes, the main purpose is to avoid hard coding the code. I used it when I was experimenting with ASP.Net and learned to store the connection in a configuration file which was automatically created in each project. It looks like I need to learn to create one myself in C#.
I intend to use the example referred by expert Anarki but I need to check if i can use SqlServer instead of MS.Access. This is the plan for the weekend and I will see if I can figure it out.
P.S. Thank your for the try-catch remark anarki_jimbel. Yes, I will use it.
ASKER
Thank you
http://msdn.microsoft.com/en-us/library/ms243192.aspx
In some cases I just create a custom xml file with required entries and read it. Depends on a task.