I would not have the "createcommand" or "createconnection" methods.
It is much better to approach each of the access methods (for example, "GetAccounts") to process their own instance of the connection / command.
Connection and Command objects implement IDisposable. Therefore, it is better to use statements in which they are created and deleted as necessary. Thus, this can lead to serious memory problems.
In addition, the CreateParameter method does not seem to provide any real benefit from simply invoking the βnew SqlParameterβ in the code that should create these parameters.
I would do the following:
public interface IDbAccess { String ConnectionString; Collection<Account> GetAccountsById(Int32 id); Boolean StoreAccount(Account acct); } public class SqlDatabase : IDbAccess { public String ConnectionString {get; set;} public SqlDatabase(String connection) { ConnectionString = connection; } public Collection<Account> GetAccountsById(Int32 id) { using (SqlConnection connect = new SqlConnection(ConnectionString)) { using (SqlCommand cmd = new SqlCommand(connect)) {
Thus, your datalayer is specific to the functions you provide. There are already excellent wrappers for accessing db, such as the Enterprise Library. The approach you take adds nothing and introduces errors.
In addition, this approach means that you can implement vendors without a database, such as XML, web services, etc. with zero code changes.
Notme source share