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:
- Client Layer (GUI)
- Business Application Layer (Logic and Validations)
- 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).
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.
Using the code
Client Side (UI)
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;
namespace Contact_System
{
public partial class signin : Window
{
public signin()
{
InitializeComponent();
txtUsername.Focus();
}
private void btnSignin_Click(object sender, RoutedEventArgs e)
{
CredantialsBAL credantials = new CredantialsBAL();
bool found = credantials.authenticate(txtUsername.Text, txtuserID.Password);
if (found)
{
Phonebook goPhonebook = new Phonebook(credantials.userID);
this.Hide();
goPhonebook.ShowDialog();
}
else if (!found)
{
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;
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;
namespace App_Code_Library.Data_Access
{
public class CredantialsDAL
{
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
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.