Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Oct 2012CPOL1 min read 20.3K   4  
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

C#
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)

C#
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)

C#
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)


Written By
Software Developer PURCO SA
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.

Comments and Discussions

 
-- There are no messages in this forum --