A great way to design your own application is to use the
N-tier architecture.
This is just a simple tip that I will share and it is for you to check out.
Note: The following layers are exposed as different projects under one solution.
Object Layer: Create a class with properties:
public class Person
{
public string Firstname {get;set;}
public string Lastname {get;set;}
public int Age {get;set;}
}
Data Access Layer: The layer that interacts with database:
public sealed class PersonDAL
{
Person person = new Person();
public static int GetAge(string firstname, string lastname)
{
try
{
SQLConnection sqlcon = new SQLConnection(ConfigurationManager
.ConnectionStrings["myDB"]
.ConnectionString);
SQLCommand sqlcmd = new SQLCommand("SELECT age FROM Person_tb WHERE
firstname=@firstname AND lastname=@lastname",
sqlconn);
sqlcmd.CommandType = CommandType.CommandText;
sqlcmd.Parameters.AddWithValue("@firstname", firstname);
sqlcmd.Parameters.AddWithValue("@lastname", lastname);
sqlcon.Open();
int retval = (int)sqlcmd.ExecuteScalar();
}
catch(Exception ex)
{
}
finally
{
sqlcmd.Dispose();
sqlcon.Close();
sqlcon = null;
}
}
}
Business Logic Layer: The layer that serves as bridge for DAL and Application Layer:
public sealed class PersonBLL
{
public static int GetAge(string firstname, string lastname)
{
return PersonDAL.GetAge(firstname, lastname);
}
}
Application/Presentation Layer: The layer where users input data:
Person person = new Person();
person.Firstname = "Eduard";
person.Lastname = "Lu";
string firstname = person.Firstname;
string lastname = person.Lastname;
txtAge.Text = PersonBLL.GetAge(firstname, lastname).ToString();
There are still a lot of ways to optimize this one. You can add more layers which your application might be needing. For example, add a Utilities Layer wherein you can place your configuration/settings and your error/transaction logger. In my example, you can create a function that will return the connection string for your application to communicate with your database server.
Also, creating stored procedure is better than using command text. In my example, I used command text just to show you my query.
Message me if you have concerns. :)