HARAMAYA UNIVERSITY
COLLEGE OF COMPUTING AND INFORMATICS
DEPARTMENT OF SOFTWARE ENGINEERING
EVENT-DRIVEN PROGRAMMING (Seng 5121)
CHAPTER SEVEN- WINDOWS FORM AND DATABASE
COMPILED BY: GIZACHEW B.
2 CONTENT
Requirements
Useful Common Class and Methods
Exercises
3 REQUIREMENTS
1. Create database
Step 1: Server explorer
Step 2: Right Click on Data Connection
Step 3: Add Connection
Device Name
New Database Name
4 REQUIREMENTS
2. CREATE TABLE
Step 1: Open the new database
Step 2: Right Click on Tables
Step 3: Add New Table
5 REQUIREMENTS
6 USEFUL COMMON CLASS AND METHODS
using System.Data.SqlClient;
string query= “SELECT * FROM Student”;
ConnectionString Gets or sets the string used to open a SQL Server database.
It is a path of database location.
string ConString= “Data Source=DESKTOP-254C50Q;Initial
Catalog=DB1;Integrated Security=True”
SqlConnection con = new The C# SqlConnection class handles database connections.
SqlConnection(ConString); It initiates a connection to your SQL database.
You need to call Open() on the SqlConnection instance before using it in an
SqlCommand.
SqlCommand cmd = new It executes SQL commands on a database.
SqlCommand (query, con); It sends an SQL command to a database that is specified by an
SqlConnection object.
Needs cmd.ExecuteNonQuery() if the query is submission query.
7 USEFUL COMMON CLASS AND METHODS
using System.Data.SqlClient;
string query= “SELECT * FROM Student”;
SqlDataReader reader = SqlDataReader class in C# is used to read data from the SQL Server database
cmd.ExecuteReader(); in the most efficient manner. It reads data in the forward-only direction.
It means once it reads a record, it will then read the next record; there is no
way to go back and read the previous record.
SqlDataAdapter adapter = new SqlDataAdapter in C# bridges a DataSet or DataTable and a Data Source
SqlDataAdapter(query, con); (SQL Server Database) to retrieve data.
-------------------------------------- It is a class that represents a set of SQL commands and a database
DataTable dt = new connection.
DataTable(); It is used to fill the DataSet or DataTable and update the data source as well.
adapter.Fill(dt); Fill(DataSet): a method in SqlDataAdapter class that used to add rows in the
DataSet to match those in the data source.
8 USEFUL COMMON CLASS AND METHODS
9 EXERCISES
STUDENT
#
Name StudID Gender
1
UJULU CEP001 Male
2
DIGNITY CEP002 Female
3
REDLINE CEP003 Male
10 EXERCISES
using System.Data.SqlClient;
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-254C50Q;Initial
Catalog=SENG5z;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", con);
DataTable dt = new DataTable();
da.Fill(dt);
//********************************************
SqlCommand cmd= new SqlCommand("SELECT * FROM Student", con);
SqlDataReader reader = cmd.ExecuteReader();
//****************************************
}
11 EXERCISES
Console.WriteLine("USING SQLDATAADAPTER | DATA TABLE");
//Active and Open connection is not required
//dt.Rows: Gets the collection of rows that belong to this table
//DataRow: Represents a row of data in a DataTable.
foreach (DataRow row in dt.Rows)
{
//Accessing using string Key Name
Console.WriteLine(row["Name"] + ", " + row["StudID"] + ", " + row["Gender"]);
//Accessing using integer index position
//Console.WriteLine(row[0] + ", " + row[1] + ", " + row[2]);
}
Console.WriteLine("---------------");
Console.WriteLine("USING SQL DATAREADER");
while (reader.Read())
{
Console.WriteLine(reader["Name"] + ", " + reader["StudID"] + ", " + reader["Gender"]);
}
12 EXERCISES
1. Create Database.
2. Create Table
3. Create the form
4. Insert Data into Table
5. Display Table data on DataGridView
6. Search By Name
13 CLASS WORK
TEACHING YOU IS A GOOD LUCK