65.9K
CodeProject is changing. Read more.
Home

Login system demonstrating 3-tier architecture and connect to IBM DB2

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Oct 23, 2012

CPOL

1 min read

viewsIcon

20555

Creating a basic login system using 3-tier architecture and connecting to IBM DB2 Express C.

Introduction

The article demonstrates how to develop applications using multiple tiers and connect an IBM DB2 database to your .NET application. The 3-tier architecture is usually known to have:

  1. Client Layer (GUI)
  2. Business Application Layer (Logic and Validations)
  3. Data Access Layer (queries /stored procedures)

The client layer will be in a separate project, and the rest of the layers will be created in your class library project (optional).

3-Tier solution

Adding a Class Library (App_Code_Library) to the Client Side (Contact_System)

Next will show you how to add references to your Class Library (App_Code_Library) to your Client Side (Contact_System). The references will be the” .DLL” files usually located in the bin>debug folder.

By doing this, it allows you to call the public classes from your Class Library (App_Code_Library) and use them in your client side.

Right click References >Add References> Browse> App_Code_Library>Bin>Debug> App_Code_Library.dll.

Adding IBM DB2.Data.dll file to your Client Side (Contact_System) .

This allows you to be able to use classes like DB2Connection, DB2Command, DB2DataReader etc.

Right click References >Add References> .NET > .Data.DB2>.

In the end your solutions should have the .dll files showed in the picture below.

References

Using the code

Client Side (UI)

References

Client-side backend code (sign in) button

using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using App_Code_Library.Business_Application;
//Reference Logic Layer to get Classes like CredantialsBAL and their methods

namespace Contact_System
{
    /// <summary>
    /// Interaction logic for signin.xaml
    /// </summary>
    public partial class signin : Window
    {
        public signin()
        {
            InitializeComponent();
            txtUsername.Focus();
        }

        private void btnSignin_Click(object sender, RoutedEventArgs e)//event to sign in
        {
            CredantialsBAL credantials = new CredantialsBAL();
            bool found = credantials.authenticate(txtUsername.Text, txtuserID.Password);
            if (found)//user exist
            {
                Phonebook goPhonebook = new Phonebook(credantials.userID);
                this.Hide();
                goPhonebook.ShowDialog();
            }
            else if (!found)//user does not Exist
            {
                MessageBox.Show("Oops! Incorrect login details", "Contact System:Error", 
                  MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}

Business Layer (Logic Layer)

using System.Data;
using System.Windows.Forms;
using App_Code_Library.Data_Access;
//Reference your Data Access Layer to get classes like CredantialsDAL

namespace App_Code_Library.Business_Application
{
    public class CredantialsBAL
    {
        public string userID;
        public  bool  authenticate(string username, string password)
        {
            CredantialsDAL dal = new Data_Access.CredantialsDAL();
            DataTable dTable = dal.getAuthentication();

            bool found = false;
            foreach (DataRow dRow in dTable.Rows)
            {
                if ((username == dRow["_username"].ToString()) && 
                             password == dRow["_password"].ToString())
                {
                    userID = dRow["userID"].ToString();
                    found = true;
                }
            }
            if 
                (!found)
            {
                return false;
            }

            return found;
           
        }    
    }
}

Data Access Layer (Queries/Stored Procedures)

using System.Linq;
using System.Text;
using System.Data;
using IBM.Data.DB2;//Add this line to refrence DB2 Classes

namespace App_Code_Library.Data_Access
{
    public class CredantialsDAL
    {
        // Your connection string to connect IBM DB2 Database 
        string connString = "Database=DBPBOOK;userID=db2admin;password=mypassword;server=localhost";

        #region Check user Exist
        public DataTable getAuthentication()
        {

            using (DB2Connection con = new DB2Connection(connString))
            {
                DB2Command cmd = new DB2Command("SELECT * FROM YourSchemaName.tbl_credantials", con);
                DataTable dTable = new DataTable();
                DB2DataAdapter adapter = new DB2DataAdapter(cmd);
                adapter.SelectCommand = cmd;
                adapter.Fill(dTable);

                return dTable;
            }
        }
        #endregion
    }
}

References