0% found this document useful (0 votes)
33 views

Visual Programming 2

The document describes a three-tier architecture application for inserting and searching data in a database. The application has a presentation tier for the user interface, a business tier for application logic, and a data tier that directly accesses the database. The business tier calls methods on the data tier to insert and retrieve data. The data tier uses ADO.NET to connect to a SQL database and execute commands.

Uploaded by

Noor Saba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Visual Programming 2

The document describes a three-tier architecture application for inserting and searching data in a database. The application has a presentation tier for the user interface, a business tier for application logic, and a data tier that directly accesses the database. The business tier calls methods on the data tier to insert and retrieve data. The data tier uses ADO.NET to connect to a SQL database and execute commands.

Uploaded by

Noor Saba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment#2

Name:
Noor Saba
Roll No:
RC-167
Class:
BSCS
Subject:
VP
Section
B
CODE:

using System;

using System.Windows.Forms;

namespace ThreeTierDemo

public partial class MainForm : Form

public MainForm()

InitializeComponent();

private void insertButton_Click(object sender, EventArgs e)

string name = nameTextBox.Text;

int age = int.Parse(ageTextBox.Text);

BusinessTier.InsertData(name, age);

MessageBox.Show("Data inserted successfully.");

private void searchButton_Click(object sender, EventArgs e)


{

string searchTerm = searchTextBox.Text;

var searchResults = BusinessTier.SearchData(searchTerm);

resultListBox.Items.Clear();

foreach (var result in searchResults)

resultListBox.Items.Add($"Name: {result.Name}, Age: {result.Age}");

using System.Collections.Generic;

namespace ThreeTierDemo

class BusinessTier

public static void InsertData(string name, int age)

DataTier.InsertData(name, age);

public static List<Person> SearchData(string searchTerm)

{
var searchResults = DataTier.SearchData(searchTerm);

return searchResults;

using System.Collections.Generic;

using System.Data.SqlClient;

namespace ThreeTierDemo

class DataTier

public static void InsertData(string name, int age)

using (var connection = new SqlConnection("<connection string>"))

connection.Open();

using (var command = new SqlCommand("INSERT INTO People (Name, Age) VALUES (@name,
@age)", connection))

command.Parameters.AddWithValue("@name", name);

command.Parameters.AddWithValue("@age", age);

command.ExecuteNonQuery();
}

public static List<Person> SearchData(string searchTerm)

var results = new List<Person>();

using (var connection = new SqlConnection("<connection string>"))

connection.Open();

using (var command = new SqlCommand("SELECT Name, Age FROM People WHERE Name LIKE
@searchTerm", connection))

command.Parameters.AddWithValue("@searchTerm", $"%{searchTerm}%");

using (var reader = command.ExecuteReader())

while (reader.Read())

string name = reader.GetString(0);

int age = reader.GetInt32(1);

results.Add(new Person { Name = name, Age = age });

}
}

return results;

You might also like