National University of Modern Languages
Department of Computer Sciences and Engineering
Visual Programming
Lab 11
Objectives
1. Database connection and operations
Database connectivity basics
C# provides connectivity options to a large number of databases. We will be using Microsoft SQL
Server database for learning.
Connection is the basis and first step of working with a database. A connection has multiple
parameters that are database name, credentials and some options (like connection time etc.).
CRUD operations can be performed on a database. The required namespaces for these
operations are mentioned below. These should be added to your project in order to perform
database operations.
using [Link];
using [Link];
SqlDataAdapter: It is a set of commands and database connection that is used for
communication between a data set/ data table and SQL Server Database.
SQLDataReader: It is used for reading data only and it works on reading data as a stream. Useful
where we have to read specific data and show it on specific controls.
Task 1: UI Design
Load the project previously created having the following user interface.
Task 2: Creating Database Table
Create an Items Table in your Database with columns as follows:
[ItemName] ,[Category] ,[Price] ,[Quantity] ,[Department] ,[Distributor] ,[DistributorContact]
Also add certain records in the table.
Task 3: Setting up Basis
Add required namespaces to your Form class.
using [Link];
using [Link];
Add the following objects as well.
Keep a data set and data table for receiving data.
Define a connecting string to be used for establishing connection.
Task 4: Establishing DB Connection
Use the following function to connect to DB (as specified in connection string) whenever
needed.
Call this function in your Form Load even and display a successful message if connection is
established.
Task 5: Reading data through SqlDataReader
Add the following two fields in your Form UI. These fields will get data from database through
SqlDataReader.
Query to be executed:
Select * from Items where ItemID = 112200
Call this function in Form Load by passing the query mentioned above.
Task 6: Reading data through SqlDataAdapter
Add the following function in your Form Class.
Call the function in your Form Load class to populate our Data Table.
Additional Task:
Add searching feature in your program that searches based on specific criteria.
Pro Tips:
DataGridView provides some default operations as adding, deleting and editing. Exploit
them wherever required in order to make your application more effective.