Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 23 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anele Ngqandu
ICT International Consulting and Training Worx
South Africa South Africa
Anele Ngqandu works at ICT International Consulting and Training Worx.
 
Have skills and knowledge in technologies such as c#,
Php, java scripting, CSS, ASP.Net 3.5 and 4 Framework, WPF,WPF Browser Application, 3- Tier Architecture, MySQL, MS SQL Server 2008,IBM DB2,SQLite, Apache Friends Xampp,Linux,IBM InfoSphere Information Server 8.5, windows phone development and project management.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 23 Oct 2012
Article Copyright 2012 by Anele Ngqandu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid