0% found this document useful (0 votes)
38 views26 pages

Event Driven 6

This document provides an overview of event driven programming and database programming concepts using C#. It defines databases and database management systems. It describes relational databases and SQL. It also explains how to connect C# projects to databases using ADO.NET and work with datasets, perform queries, and handle errors.

Uploaded by

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

Event Driven 6

This document provides an overview of event driven programming and database programming concepts using C#. It defines databases and database management systems. It describes relational databases and SQL. It also explains how to connect C# projects to databases using ADO.NET and work with datasets, perform queries, and handle errors.

Uploaded by

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

Event Driven Programming

Hawassa University Daye Campus


DEPARTMENT OF COMPUTER SCIENCE

Chapter 6
Database Programming Compiled by Talegeta G.
01/29/2024 Event Driven Programming with C# 1
Database System
 A database is
 a collection of related,

 logically coherent data used by the application programs in an organization.

 Advantages of databases
 Less redundancy

 Inconsistency avoidance

 Efficiency

 Data integrity

 Confidentiality
01/29/2024 Event Driven Programming with C# 2
DBMS
 A database management system (DBMS)
 defines,
 creates and
 maintains a database.
 allows controlled access to data in the database.
 A DBMS is a combination of five components:
 hardware,
 software,
 data,
 users and
 procedures

01/29/2024 Event Driven Programming with C# 3


Relational Database Model
 Data are organized in two-dimensional tables called relations.

 The tables are related to each other.

 A relation in an RDBMS has the following features:


 Name of entity
 Attributes
 Tuples

01/29/2024 Event Driven Programming with C# 4


Relational Integrity Constraints
 Constraints are conditions that must hold on all valid relation states.
 There are three main types of constraints in the relational model:
 Key constraints
> Primary Key
> Unique Key
> Foreign Key
 Entity integrity constraints
 Referential integrity constraints
 Another implicit constraint is the domain constraint
 Every value in a tuple must be from the domain of its attribute (or it could be null, if
allowed for that attribute)
 Check constraints
 Default value constraint
01/29/2024 Event Driven Programming with C# 5
Essential SQL Skill
 Creating and/or modifying database objects
 Retrieving data from a table
 Insert, Delete, update data
 Using functions
 String function
 Numeric functions
 Date/Time functions
 Other functions
> CASE, IF, IFNULL
 Creating View
 Transact-SQL programming
 Scripts
 Creating triggers
 Stored procedures
 Functions
 Working with Transaction
01/29/2024 Event Driven Programming with C# 6
ADO.NET
Connecting a project to Database system and access database objects

01/29/2024 Event Driven Programming with C# 7


ADO.NET
 ActiveX Data Object .NET is the primary data access API for the .NET Framework
 provides the classes that you as you develop database application with C# and other .Net
languages
These classes can be divided into two
 The .NET data provider
 which provide the classes that you use to access the data in database
 Dataset
 which provide the classes that you can use to store and work with data in your
application

01/29/2024 Event Driven Programming with C# 8


The .NET data providers
 A .NET data provider is a set of classes that enables you to access data that’s managed by a
particular database server.
 All .NET data providers must include core classes for creating the four types of objects.
 Connection
> establishes a connection to a database server
 Command
> represents an individual SQL statement that can be executed against the database
 Data reader
> provides read-only forward access to the data in a database
 Data adapter
> provides the link between the command and connection objects and a dataset object

01/29/2024 Event Driven Programming with C# 9


The .NET data providers
Data provider included with the .NET Framework
 SQL Server
 System.Data.SqlClient
> enables access MS SQL Server database
 OLEDB (Object Linking and Embedding, Database)
 System.Data.OleDb
> enables access to any database that supports OLEDB
 ODBC (Open Database Connectivity)
 System.Data.Odbc
> enables access to any database that supports ODBC
 Oracle
 System.Data.OracleClient
 Oracle.DataAccess.Client;
 enables access to Oracle database
Class name for data provider
Object SQL Server OLEDB ODBC Oracle
 Connection SqlConnection OleDbConnection OdbcConnection OracleConnection
 Command SqlCommand OleDbCommand OdbcCommand OracleCommand
 Data Reader SqlDataReader OleDbDataReade OdbcDataReader OracleDataReader
 Data adapter SqlDataAdapter OleDbDataAdapter OdbcDataAdapter OracleDataAdapter
01/29/2024 Event Driven Programming with C# 10
ADO.NET….
The steps for executing database command in ADO.NET are:
 Create connection object
 Open the connection
 Create a command object encapsulating both Sql command and the connection
 Call a method on the command object to execute the command
 Close the connection by calling the close on the connection object

You can create ADO.NET objects


 By Writing the code yourself
 From data source listed in the Data Sources window

 Add Data Source using Data Source Configuration wizard


01/29/2024 Event Driven Programming with C# 11
Creating ADO.NET MS SQL Server Database objects
 Create a connection object
 The SqlConnection Object is handling the part of physical communication between the C# application and
the SQL Server Database.
 An instance of the SqlConnection class in C# is supported the Data Provider for SQL Server Database.
 The SqlConnection instance takes Connection String as an argument and passes the value to the
Constructor statement.
 connetionString = "Data Source=ServerName; Initial Catalog=DatabaseName; User
ID=UserName; Password=Password“;
conn = new SqlConnection(connetionString);
cnn.Open();
 When the connection is established,
 SQL Commands will execute with the help of the Connection Object and retrieve or manipulate the
data in the database.
 Once the Database activities over,
 Connection should be closed and release the Data Source resources.
 conn.Close();
01/29/2024 Event Driven Programming with C# 12
Create a command object
 The Command Object in ADO.NET executes
 SQL statements and

 Stored Procedures against the data source specified in the C# Connection Object.

 The Command Object requires an instance of a C# Connection Object for


executing the SQL statements.

 Some important built in methods uses in the Command Object to execute the
SQL statements

01/29/2024 Event Driven Programming with C# 13


Working with Data Source and Datasets
 Data source
 Specifies the source of data for an application

 shows all the tables and columns in the dataset that are available to your
application
 Easiest way to generate Windows forms that work with the database

 Especially for small application or prototyping large application

01/29/2024 Event Driven Programming with C# 14


Data Source and Datasets …
 Create a data source
 Open the Data Source windows
 Click on the Data Source tab or Data ->Data Source command
 Click on the Add New Data Source link, if your adding data source for the first time to your
project, or
 use Data->Add New Data Source command
 Choose the data source type, Click Next
 Choose the database model, (Dataset or Entity Data Model), select dataset, click Next
 Choose the data connection, Click New Connection -> select Data source and click Continue
 Add Connection: Select/Enter database server, Select/Attach Database, click Ok
 Choose your data connection, have a look at your connection string, click Next
 Save connection string to application configuration file, Click Next
 Choose Database object you need to add to your project
 Click Finish, Enjoy with drag and drop of your database object
01/29/2024 Event Driven Programming with C# 15
Handling errors
 Data errors
 Sql Exception

 ADO.Net error
 DB Concurrency Exception

 Data Exception

 Constraint Exception

 No Null Allowed Exception

 Data Grid View data error


 Data Error Event handler
01/29/2024 Event Driven Programming with C# 16
DataSet and DataAdapters
 ADO.NET’s SqlDataReader class provides stream-based access to the results of database
queries
 Streaming access is fast and efficient, but it’s also read-only and forward-only.
 ADO.NET supports set-based data access as well as stream-based data access.
 Set-based accesses capture an entire query in memory and support backward and forward
traversal through the result set.
 edit the data obtained through database queries, propagate the changes back to the data
source, and much, much more.
 Set-based data accesses revolve around two classes:
 Data Set, which is the equivalent of an in-memory database and is defined in the
System.Data namespace, and
 Data Adapter, which serves as a bridge between DataSets and physical data sources.

01/29/2024 Event Driven Programming with C# 17


DataSet
 DataSet provides a disconnected representation of result sets from the Data Source,
and it is completely independent from the Data Source.

 DataSet provides much greater flexibility when dealing with related Result Sets.

 Dataset is structured much like a relational database.

 It can contain one or more tables, and each table can contain one or more columns
and rows.

01/29/2024 Event Driven Programming with C# 18


DataSet …

01/29/2024 Event Driven Programming with C# 19


DataSet …
 You can also work with the data in a database without using a data adapter.
 Use command and connection objects to access database.
 Instead of using a data adapter to execute the commands, you can execute the commands
directly.
 When you do that, you also have to provide code to handle the result of the command.
 If you issue a command that contains an
 Insert, Update, or Delete statement
> the result is an integer that indicates the number of rows that were affected by the
operation.
 You can use that operation to determine if the operation was successful.
 If you execute a command that contains a Select statement,
 the result is a result set that contains the rows you requested.
 To read through the rows in the result set, use a data reader object
01/29/2024 Event Driven Programming with C# 20
DataSet Class
 Think of a DataSet as an in-memory database.
 The actual data is stored in DataTable objects, which are analogous to tables in a database.
 The DataSet.Tables property exposes a list of the DataTables in a DataSet.
 Records in a DataTable are represented by DataRow objects, and fields are represented by
instances of DataColumn.
 DataTable properties named Rows and Columns expose the collections of DataRows and
DataColumns that make up the table.
> DataSet ds = new DataSet ();
> // TODO: Initialize the DataSet
> MyDataGrid.DataSource = ds;
> MyDataGrid.DataBind ();

01/29/2024 Event Driven Programming with C# 21


DataSets vs. DataReaders
 DataReader
 simply to query a database and read through the records one at a time until you find the one you’re
looking for,
 retrieve only the data that you actually use, and they don’t consume memory by storing every record
that you read.
 DataSet
 to use all the query results (perhaps because you’re displaying them in a table), you need the ability to
iterate backward and forward through a result set
 to cache the result set in memory
 DataSet ds = new DataSet ();
 // TODO: Initialize the DataSet
 MyDataGrid.DataSource = ds;
 MyDataGrid.DataBind ();
 the same code can be implemented more efficiently with a DataReader:
 SqlDataReader reader = cmd.ExecuteReader ();
 MyDataGrid.DataSource = reader;
 MyDataGrid.DataBind ();
01/29/2024 Event Driven Programming with C# 22
DataSet …
 purpose is to perform database queries and create DataTables
containing the query results.
 It’s also capable of writing changes made to the DataTables
back to the database.
 Initializing a DataSet from a Database: DataAdapter.Fill

01/29/2024 Event Driven Programming with C# 23


DataAdapter Classes
 SqlDataAdapter adapter = new SqlDataAdapter ("select * from course", conn);
 DataSet ds = new DataSet ();
 adapter.Fill (ds, “Course");
 Fill opens a connection to the database using the connection string passed to
SqlDataAdapter’s constructor.
 It performs a query on the Pubs database using the query string passed to
SqlDataAdapter’s constructor.
 It creates a DataTable named “Course” in the DataSet.
 It initializes the DataTable with a schema that matches that of the “Course” table in
the database.
 It retrieves all the records produced by the query and writes them to the DataTable.
 It closes the connection to the database.
01/29/2024 Event Driven Programming with C# 24
For more information
 Anne Boehm, Ged Mead. ADO.NET 4 Database programming
with C# 2010
 Joel Murach, Anne Boehm. Murach C# 2012, Mike Murach &
Associates Inc USA, 2013
 Deitel, C#-How to Program. USA, 2010
 Svetlin Nakov et al. Fundamentals of Computer Programming
With C#. Sofia, 2013
 www.csharp.net-informations.com

01/29/2024 Event Driven Programming with C# 25


.

Question!
?

You might also like