Upload
ammara-arooj
View
59
Download
3
Embed Size (px)
Citation preview
Objectives
Designing Back End DatabaseFront End Design using ASP.NETWriting Event ProceduresFront End-Back End ConnectivityExecuting SQL Queries from Front End
Database
Collection of DataSpecific to a TopicEfficient in Storage and RetrievalCompactnessPhysical Collection of Data on Storage
Media
DBMS
Data Base Management SystemCollection of ProgramsFunctions:
Creation + Management of DatabaseCreation + Management of Database
Table StructureStorage of DataManipulation of Data
RDBMS
Relational Data Base Management System
Tabular DBMSData Storage in form of TablesTable-Collection of:
Column: Vertical, Field, PropertyRow: Horizontal, Record, Collection of
Field
Table
RollNo StName StDOB StEmail
2009-ag-2533 Ali Ahmad 25/12/1986 [email protected]
2010-ag-2686 Jawad Ahmad 23/05/1987 [email protected]
2011-ag-1101 Shahid Nazir 05/06/1987 [email protected]
2005-ag-4156 Junaid Ali 03/02/1982 [email protected]
Fields/Column
Record/Row
Designing Back End
Designing Back End Involves:Creating DatabaseCreating TablesSpecifying Columns/FieldsEach Have Its Unique NameTools:
SQL Server 2008 Compact EditionSQL Server 2008 Management Studio
SQL Server Compact Edition
Integrated with Visual Studio 2010Service Name: SQLEXPRESSDatabase Extension: .sdf, .mdfSuitable for PC Database Applications
.sdf: SQL Server Compact Database File.mdf: SQL Server Database File
SQL Server 2008 Management StudioIndependent InstallationDefault Service Name:
MSSQLSERVERDatabase Extension: .mdfSuitable for Client/Server Applications
.mdf: SQL Server Database File
1
Open Visual Studio 2010Go to “Data Connections” in “Server
Explorer”Right Click “Data Connections” and
Select “Add Connection”“Add Connection” Dialog Box
4
Go to “Change” Portion of “Add Connection” Dialog Box
“Change Data Source” Dialog BoxSelect “Microsoft SQL Server
Database File” and Press “OK”“Add Connection” Dialog Box AppearsType in the Path and Database File
Name and Press “OK”
1
Go to “Data Connections” in “Server Explorer” Area
Database.mdf will be presentDouble Click DatabaseRight Click on “Table”Click “Add New Table”
3Set The Field “st_regno” as Primary
KeyRight Click on “st_regno” Field and
Select “Set Primary Key”Press CTRL+SType Table Name “student_info” and
Press “OK”Table will be Created
Front End
User Interface (GUI)User`s Working AreaUsed to Send Commands to DBMSDBMS Executes Commands on
DatabaseData EntryData ManipulationData/Information Retrieval
Web Form (1)
Main ContentPage AreaPlace to Hold other ContentsBase of Application GUISingle Web Site may Contain as many
Web Forms as Required
Designing Front End
Selection of Controls RequiredPlacement on Form ControlChanging PropertiesCustomization of Controls:
FontResizeColoringAlignment
Event
Happening of AnythingDifferent Controls have Different
EventsSingle Left ClickDouble Left ClickMouse MoveKey Press
Procedure
Piece of CodePerforms Some OperationProgram can have Many ProceduresProcedure and Event Combination
Writing Event Procedures
Current Scenario:Save Data Button – Click
Database Connectivity and Query to Save Data in Database (INSERT Query)
Clear Form Button – ClickClear Form
Exit Button – ClickExiting the Application
Connectivity
Importing NamespaceUsing SqlConnection ClassConnectionStringOpening ConnectionChecking Connection StateClosing Connections
Importing Namespaces
Importing Namespaces in General Declaration Section using Keyword ‘Imports’:System.DataSystem.Data.SqlSystem.Data.SqlClient
SqlConnection Class
Represents Unique Connection to Database
Used to Open Connection with Database
Must be Declared and InitializedObject Instance CreationContain Properties and Methods
ConnectionString
Important PropertyProperty Name: ConnectionStringMust be Set Properly to Open
ConnectionString TypeString Required to Open A Connection
to SQL Server Database
ConnectionString Parts
Data Source:Server Name or Service Namee.g. SQLEXPRESS
AttachDbFilename:Complete Path of Database File
Integrated Security:Security PasswordTRUE or FALSE
Connect Timeout:Time to Cancel Query in case of Error
Opening Connection
Open() Method of SqlConnection Class
Used to Open a Connection to Database
Connection Must be Open Before Manipulating Database
Checking Connection State
State Property of SqlConnection Class
Boolean Property (TRUE or FALSE)Used to Check Status of ConnectionOutcomes:
TRUE (Opened Connection)FALSE (Closed Connection)
Closing Connection
Close() Method of SqlConnection Class
Closes Connection to DatabaseConnection Must be Closed and
Reopened After Performing Database Manipulation
SQL Queries
SQL—Structured Query LanguageQuery—Any QuestionExecuted on Database by DBMSSent By API (Application Program
Interface)
SQL Query Types
DDL:Database CreationTable Structure Management
DML:INSERT (Entering New Data)UPDATE (Update Existing Data)DELETE (Deleting Existing Data)SELECT (Retrieving Data)
Query Execution
Importing NamespaceUsing SqlCommand ClassInitializing Command Text and
Registering ConnectionExecuting INSERT QueryUsing SqlDataReader, SqlDataAdapter
ClassesRetrieving and Showing Data
Importing Namespaces
Importing Namespaces in General Declaration Section using Keyword ‘Imports’:System.DataSystem.Data.SqlSystem.Data.SqlClient
SqlCommand Class
Deals with DatabaseExecutes SQL CommandsSpecification of SqlConnectionMust be Declared and InitializedCollection of Properties and Methods
Important Members
SqlCommand(String,Connection) – Constructor
CommandTextConnectionExecuteReader()ExecuteNonQuery()Cancel()
SqlCommand(String,Connection)
ConstructorSqlCommand(String,Connection)Instantiates SqlCommand Object with
String CommandText and Connection SqlConnection
CommandText
PropertyGets or Sets
SQL Command to be Executed by SqlCommand through SqlConnection
Command can be:INSERTUPDATEDELETESELECT
String Type
Connection
PropertyGets or Sets
SQL Connection to Execute SQL Command
Command can be:INSERTUPDATEDELETESELECT
SqlConnection Type
ExecuteReader()
MethodExecute Commands Returning Rows
i.e. SELECT QueryExecutes SELECT SqlCommandReturn Rows and Store them in
SqlDataReader Object
ExecuteNonQuery()
MethodExecutes Commands Not Retrieving
Rows i.e. INSERT, UPDATE, DELETEExecutes INSERT/UPDATE/DELETE
SqlCommandDoesn`t Return Anything
Recipe….
Open and Test Connection Using SqlConnection Object
SqlCommand Object CreationSet CommandText and Connection
PropertiesExecute Method ExecuteNonQuery()Display Message Accordingly
Recipe….
Open and Test Connection Using SqlConnection Object
SqlCommand Object CreationSet CommandText and Connection
PropertiesExecute Method ExecuteReader() and
Store Data in SqlDataReader ObjectDisplay Data
SqlDataReader Class
Reads Data Rows Directly from SQL Server Database
Reading Order—ForwardLoops – To Read Multiple RowsEfficientContains Properties and MethodsMust be Open to Read Data
HasRows
PropertyBoolean ValuesIndicates Whether Rows are Returned
in Response to a SELECT QueryOutcomes:
TRUE – Rows ReturnedFALSE – No Row Returned
IsClosed
PropertyBoolean ValuesIndicates Whether SqlDataReader is
Closed or NotOutcomes:
TRUE – ClosedFALSE -- Open
Close()
MethodCloses SqlDataReader ObjectSqlDataReader Object Must be
Closed to Execute Another SQL Command and to Return Data in the Same Reader