Click here to Skip to main content
12,950,268 members (65,541 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.8K views
4 bookmarked
Posted 23 Oct 2012

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
Software Developer (Junior) Synapsis Software Pty Ltd
South Africa South Africa
Anele Ngqandu works at Synapsis Software Pty Ltd.

Have skills and knowledge in technologies such as c#,jQuery,Angular1, Html,CSS, ASP.Net & Framework,ASP.NetBoilerplate,MS SQL Server,Wamp Server and Project Management.

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 23 Oct 2012
Article Copyright 2012 by Anele Ngqandu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid