Greetings,
I'm trying to learn how to write a BLL and DAL to achieve a simple example of returning a user to a form. I believe I have finished 98 % of the code. I need another set of eyes on it to confirm I'm doing this right and to help make it work. This is the code in the logical call of methods or libraries. I am using one additional library 'Common' which will get/set properties:
private void Form1_Load(object sender, EventArgs e) { string CurrentUser = null; CurrentUser = GetUser(); // DO WORK WITH USER } public string GetUser() { string strUser = null strUser = BLL.GetUser(); return strUser; } using System; using System.Linq; using System.Text; using System.Data.Sql; using System.Data.SqlTypes; using System.Data.SqlClient; using System.Collections.Generic; using DAL; using Common; namespace BLL { public abstract class CompanyBase { // SETUP DATABASE CONNECTION protected CompanyDatabaseUtil SqlUtil = null; protected CompanySysFile sys = null; protected CompanyLogin login = null; protected SqlConnection sqlConnection1 = null; private SqlCommand q_command = null; private string SQLUsername = null; public CompanyBase(SqlConnection conn) { this.sqlConnection1 = conn; login = new CompanyLogin(this.sqlConnection1); SqlUtil = new CompanyUtil(this.sqlConnection1); login.Login(); sys = new CompanySysFile(this.sqlConnection1); q_command = new SqlCommand(); q_command.Connection = this.sqlConnection1; SQLUsername = login.GetSqlUsername(); } } public class UserBLL : CompanyBase, IDisposable { public UserBLL(SqlConnection sqlConnection) : base(sqlConnection) { } // GET DATA DO LOGIC public User GetUser(string SQLUsername) { try { using (var db = new DAL.RecordUser()) { var user = (from u in db.FetchUser(SQLUsername) where u.RecordUser == SQLUsername && u.Active == true select u).First(); } // DO SOME LOGIC HERE } catch (Exception ex) { } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DAL { public class RecordUser { // RETURNS A STRING OR USER OR A 'RETURN' IS NOT NECESSARY TO ACCESS THE OBJECT ?? public strUser FetchUser(string CurrentUser) { try { SqlDataReader qr = null; q_Command.CommandText = "select * from users where username = @username "; q_Command.Parameters.Clear(); q_Command.Parameters.Add("@username", SqlDbType.VarChar).Value = CurrentUser; ??? } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Common { public class RecordUsers { string RecordUser { get; set; } } }
Not sure how I should incorporate the Common library. I'm guessing after the user is obtained I would set the proerty in the BLL??
Please no articles. Thank you in advance !!
John