Hi All,
Brand new to the forum, I have been reading for a short while and have found lots of intresting threads that have helped me begin with .NET and specifically (C#) so thanks for that!
Anyways, here is what I have been up to while learning... I am writing a windows forms application that utilizes an SQL SERVER EXPRESS database to store and retreive data.
What I would like your views on really, are the best practices when retreiving data from the database... at the moment I have a class which basically contains a set of methods that I use to retreive different bits of data from my database... for example...
In the above code, I have a table called Receipts which contains an eventid and an amount... what it does is grab all of the receipts for that event in the database and adds them together, finally returning the result.
(Im sure its not amazingly pretty code but it works).
Now.... this class holds lots of similar methods that return various bits of data from the database, not all of them decimals... I have a function that returns the event name for example, as a string.
Obviously, each time a method is called... it creates a new connection to the database, sends the SELECT statement, grabs the result and returns it... is this good practice? Creating a connection to the database in the above way everytime I want some data from the database? it seems logical to me to do it this way, mainly becuase it works I guess... but just wanted to get some views on how others would do the same transactions.
This line...
This is grabbing my connection string from the app Configuration, and is accessable by the whole class so each method just calls the variable connectionString when it needs to create the connection, I dont declare this more than once in the whole app.
I hope this is clear to you all, and I look forward to your replys!
Thanks in advance!
Ben
Brand new to the forum, I have been reading for a short while and have found lots of intresting threads that have helped me begin with .NET and specifically (C#) so thanks for that!
Anyways, here is what I have been up to while learning... I am writing a windows forms application that utilizes an SQL SERVER EXPRESS database to store and retreive data.
What I would like your views on really, are the best practices when retreiving data from the database... at the moment I have a class which basically contains a set of methods that I use to retreive different bits of data from my database... for example...
Code:
private string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
public decimal getReceiptsTotal(int eventID)
{
string source = connectionString;
string select = "SELECT amount FROM receipts WHERE eventid="+ eventID;
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
SqlDataReader reader = cmd.ExecuteReader();
decimal total = 0.00m;
while (reader.Read())
{
total = total + (decimal)reader[0];
}
conn.Close();
return total;
In the above code, I have a table called Receipts which contains an eventid and an amount... what it does is grab all of the receipts for that event in the database and adds them together, finally returning the result.
(Im sure its not amazingly pretty code but it works).
Now.... this class holds lots of similar methods that return various bits of data from the database, not all of them decimals... I have a function that returns the event name for example, as a string.
Obviously, each time a method is called... it creates a new connection to the database, sends the SELECT statement, grabs the result and returns it... is this good practice? Creating a connection to the database in the above way everytime I want some data from the database? it seems logical to me to do it this way, mainly becuase it works I guess... but just wanted to get some views on how others would do the same transactions.
This line...
Code:
private string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
This is grabbing my connection string from the app Configuration, and is accessable by the whole class so each method just calls the variable connectionString when it needs to create the connection, I dont declare this more than once in the whole app.
I hope this is clear to you all, and I look forward to your replys!
Thanks in advance!
Ben