VTP Asp - Net (16-30)
VTP Asp - Net (16-30)
VTP Asp - Net (16-30)
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Class-.net Topic - Course Content Date-10/06/2024
Lecture 16 :
A login is a simple credential for accessing SQL Server. For example, you provide your username
and password when logging on to Windows or even your e-mail account. This username and
password builds up the credentials. Therefore, credentials are simply a username and a
password.
In this tutorial, we are interested in logins based on Windows Credentials and logins specific to
SQL Server.
Logins based on Windows credentials allow you to log in to SQL Server using a Windows
username and password. If you need to create your own credentials (username and password,)
you can create a login specific to SQL Server.
To create, alter, or remove a SQL Server login, you can take one of two approaches −
Step 1 − After connecting to SQL Server Instance, expand logins folder as shown in the following
snapshot.
Step 2 − Right-click on Logins, then click Newlogin and the following screen will open.
Step 3 − Fill the Login name, Password and Confirm password columns as shown in the above
screen and then click OK.
To create login name with TestLogin and password ‘P@ssword’ run below the following query.
Lecture 17 :
Database is a collection of objects such as table, view, stored procedure, function, trigger, etc.
•System databases
•User Databases
System Databases
System databases are created automatically when we install MS SQL Server. Following is a list
of system databases −
•Master
•Model
•MSDB
•Tempdb
•Resource (Introduced in 2005 version)
•Distribution (It’s for Replication feature only)
User Databases
User databases are created by users (Administrators, developers, and testers who have access
to create databases).
OR
Restore Database <Your database name> from disk = '<Backup file location + file name>
Example
OR
Note − D:\backup is location of backup file and Testdb_full_backup.bak is the backup file name
Connect to SQL Server instance and right-click on the databases folder. Click on new database
and the following screen will appear.
Lecture 18:
To run a query to select backup history on database called ‘msdb’, select the msdb database as
shown in the following snapshot.
To run your query to select backup history on database called ‘msdb’, select the msdb database
by executing the following query.
The query will open msdb database. You can execute the following query to select backup
history.
To remove your database from MS SQL Server, use drop database command. Following two
methods can be used for this purpose.
Following is the basic syntax for removing database from MS SQL Server.
Connect to SQL Server and right-click the database you want to remove. Click delete command
and the following screen will appear.
Click OK to remove the database (in this example, the name is Testdb as shown in the above
screen) from MS SQL Server.
Backup is a copy of data/database, etc. Backing up MS SQL Server database is essential for
protecting data. MS SQL Server backups are mainly three types − Full or Database, Differential
or Incremental, and Transactional Log or Log.
Backup database can be done using either of the following two methods.
The following command is used for full backup database called 'TestDB' to the location 'D:\'
with backup file name 'TestDB_Full.bak'
The following command is used for differential backup database called 'TestDB' to the location
'D:\' with backup file name 'TestDB_diff.bak'
The following command is used for Log backup database called 'TestDB' to the location 'D:\'
with backup file name 'TestDB_log.trn'
Step 1 − Connect to database instance named 'TESTINSTANCE' and expand databases folder as
shown in the following snapshot.
Step 2 − Right-click on 'TestDB' database and select tasks. Click Backup and the following screen
will appear.
Step 3 − Select backup type (Full\diff\log) and make sure to check destination path which is
where the backup file will be created. Select options at the top left corner to see the following
screen.
Step 4 − Click OK to create 'TestDB' database full backup as shown in the following snapshot.
Lecture 19:
Restoring is the process of copying data from a backup and applying logged transactions to the
data. Restore is what you do with backups. Take the backup file and turn it back into a
database.
The Restore database option can be done using either of the following two methods.
Method 1 – T-SQL
Syntax
Restore database <Your database name> from disk = '<Backup file location + file name>'
Example
The following command is used to restore database called 'TestDB' with backup file name
'TestDB_Full.bak' which is available in 'D:\' location if you are overwriting the existed database.
If you are creating a new database with this restore command and there is no similar path of
data, log files in target server, then use move option like the following command.
Make sure the D:\Data path exists as used in the following command for data and log files.
RESTORE DATABASE TestDB FROM DISK = 'D:\ TestDB_Full.bak' WITH MOVE 'TestDB' TO
'D:\Data\TestDB.mdf', MOVE 'TestDB_Log' TO 'D:\Data\TestDB_Log.ldf'
Method 2 – SSMS (SQL SERVER Management Studio)
Step 2 − Select device radio button and click on ellipse to select the backup file as shown in the
following snapshot.
Step 4 − Select Files option which is on the top left corner as shown in the following snapshot.
Step 5 − Select Options which is on the top left corner and click OK to restore 'TestDB' database
as shown in the following snapshot.
To create user name 'TestUser' with mapping to Login name 'TestLogin' in TestDB database, run
the following query.
Where 'TestLogin' is the login name which was created as part of the Login creation
Note − First we have to create Login with any name before creating a user account.
Step 1 − Connect SQL Server and expand databases folder. Then expand database called
'TestDB' where we are going to create the user account and expand the security folder. Right-
click on users and click on the new user to see the following screen.
Step 2 − Enter 'TestUser' in the user name field and click on ellipse to select the Login name
called 'TestLogin' as shown in the following snapshot.
Step 3 − Click OK to display login name. Again click OK to create 'TestUser' user as shown in the
following snapshot.
Lectures 20:
MS SQL Server provides the following two services which is mandatory for databases creation
and maintenance. Other add-on services available for different purposes are also listed.
• SQL Server
• SQL Server Agent
Other Services
• SQL Server Browser
• SQL Server Full Text Search
• SQL Server Integration Services
• SQL Server Reporting Services
• SQL Server Analysis Services
Start Services
To start any of the services, either of the following two methods can be used.
Method 1 – Services.msc
Step 1 − Go to Run, type services.msc and click OK. The following screen appears.
Step 2 − To start service, right-click on service, click Start button. Services will start as shown in
the following snapshot.
Start → All Programs → MS SQL Server 2012 → Configuration Tools → SQL Server configuration
manager.
Step 2 − Select the service name, right-click and click on start option. Services will start as
shown in the following snapshot.
Stop Services
To stop any of the services, either of the following three methods can be used.
Method 1 - Services.msc
Step 1 − Go to Run, type services.msc and click OK. The following screen appears.
Step 2 − To stop services, right-click on service and click Stop. The selected service will be
stopped as shown in the following snapshot.
Step 2 − Select the service name, right-click and click Stop option. The selected service will be
stopped as shown in the following snapshot.
Step 2 − Right-click on instance name and click Stop option. The following screen appears.
Step 3 − Click Yes button and the following screen will open.
Step 4 − Click Yes option on the above screen to stop SQL Server agent service. The services will
be stopped as shown in the following screenshot.
Lecture 21:
SQL Server data type is an attribute that specifies types of data of any object. Each column,
variable and expression has related data type in SQL Server. These data types can be used while
creating tables. You can choose a particular data type for a table column based on your
requirement.
SQL Server offers seven categories including other category of data types for use.
tinyint 0 255
bit 0 1
Numeric and decimal are Fixed precision and scale data types and are functionally equivalent.
datetimeoffset(100 nanoseconds
Jan 1, 0001 Dec 31, 9999
accuracy. Introduced in SQL Server
2008)
datetime2(100 nanoseconds
Jan 1, 0001 Dec 31, 9999
accuracy. Introduced in SQL Server
2008)
char
1
Fixed-length non-Unicode character data with a maximum length of
8,000 characters.
2
varchar
Varchar (max)
3
Variable-length non-Unicode data with a maximum length of 231
characters (Introduced in SQL Server 2005).
text
4
Variable-length non-Unicode data with a maximum length of
2,147,483,647 characters
Unicode Character Strings
Sr.No Type & Description
nchar
1
Fixed-length Unicode data with a maximum length of 4,000
characters.
nvarchar
2
Variable-length Unicode data with a maximum length of 4,000
characters.
Nvarchar (max)
3
Variable-length Unicode data with a maximum length of
230 characters (Introduced in SQL Server 2005).
ntext
4
Variable-length Unicode data with a maximum length of
1,073,741,823 characters.
Binary Strings
Sr.No Type & Description
1 binary
Fixed-length binary data with a maximum length of 8,000 bytes.
2 varbinary
Variable-length binary data with a maximum length of 8,000 bytes.
3
varbinary(max)
image
4
Variable-length binary data with a maximum length of 2,147,483,647
bytes.
Other Data Types
• sql_variant − Stores values of various SQL Server-supported data types, except text,
ntext, and timestamp.
• timestamp − Stores a database-wide unique number that gets updated every time a row
gets updated.
• uniqueidentifier − Stores a globally unique identifier (GUID).
• xml − Stores XML data. You can store XML instances in a column or a variable
(Introduced in SQL Server 2005).
• cursor − A reference to a cursor.
• table − Stores a result set for later processing.
• hierarchyid − A variable length, system data type used to represent position in a
hierarchy (Introduced in SQL Server 2008).
Lecture 22:
Creating a basic table involves naming the table and defining its columns and each column's
data type.
The SQL Server CREATE TABLE statement is used to create a new table.
Syntax
CREATE TABLE is the keyword telling the database system what you want to do. In this case,
you want to create a new table. The unique name or identifier for the table follows the CREATE
TABLE statement. Then in brackets comes the list defining each column in the table and what
sort of data type it is. The syntax becomes clearer to understand with the following example.
A copy of an existing table can be created using a combination of the CREATE TABLE statement
and the SELECT statement. You can check complete details at Create Table Using another Table.
Example
In this example, let’s create a CUSTOMERS table with ID as primary key and NOT NULL are the
constraints showing that these fields cannot be NULL while creating records in this table −
You can verify if your table has been created successfully by looking at the message displayed
by the SQL server, otherwise you can use the following command −
You can now see that CUSTOMERS table is available in your database which you can use to
store required information related to customers.
Lecture 23:
The SQL Server INSERT INTO statement is used to add new rows of data to a table in the
database.
Syntax
Where column1, column2,...columnN are the names of the columns in the table into which you
want to insert data.
You need not specify the column(s) name in the SQL query if you are adding values for all the
columns of the table. But make sure the order of the values is in the same order as the columns
in the table. Following is the SQL INSERT INTO syntax −
Example
Syntax
You can create a record in CUSTOMERS table using second syntax as follows −
All the above statements will produce the following records in CUSTOMERS table −
You can populate data into a table through SELECT statement over another table provided
another table has a set of fields, which are required to populate first table. Following is the
syntax −
Lecture 24:
In real life, we store our data in multiple logical tables that are linked together by a common key
value in relational databases like SQL Server, Oracle, MySQL, and others. As a result, we
constantly need to get data from two or more tables into the desired output based on some
conditions. We can quickly achieve this type of data in SQL Server using the SQL JOIN clause. This
article gives a complete overview of JOIN and its different types with an example.
The join clause allows us to retrieve data from two or more related tables into a meaningful
result set. We can join the table using a SELECT statement and a join condition. It indicates how
SQL Server can use data from one table to select rows from another table. In general, tables are
related to each other using foreign key constraints.
o Choose columns from each table that should be used in the join. A join condition indicates
a foreign key from one table and its corresponding key in the other table.
o Specify the logical operator to compare values from the columns like =, <, or >.
SQL Server mainly supports four types of JOINS, and each join type defines how two tables are
related in a query. The following are types of join supports in SQL Server:
1. INNER JOIN
2. SELF JOIN
3. CROSS JOIN
4. OUTER JOIN
INNER JOIN
This JOIN returns all records from multiple tables that satisfy the specified join condition. It is the
simple and most popular form of join and assumes as a default join. If we omit the INNER
keyword with the JOIN query, we will get the same output.
The following visual representation explains how INNER JOIN returns the matching records
from table1 and table2:
The following syntax illustrates the use of INNER JOIN in SQL Server:
SELECT columns
FROM table1
INNER JOIN table2 ON condition1
INNER JOIN table3 ON condition2
Let us first create two tables "Student" and "Fee" using the following statement:
Next, we will insert some records into these tables using the below statements:
Table: Student
Table: Fee
Lecture 25:
CROSS JOIN
CROSS JOIN in SQL Server combines all of the possibilities of two or more tables and returns a
result that includes every row from all contributing tables. It's also known as CARTESIAN
JOIN because it produces the Cartesian product of all linked tables. The Cartesian product
represents all rows present in the first table multiplied by all rows present in the second table.
The below visual representation illustrates the CROSS JOIN. It will give all the records
from table1 and table2 where each row is the combination of rows of both tables:
The following syntax illustrates the use of CROSS JOIN in SQL Server:
SELECT column_lists
FROM table1
CROSS JOIN table2;
Example
OUTER JOIN
OUTER JOIN in SQL Server returns all records from both tables that satisfy the join condition. In
other words, this join will not return only the matching record but also return all unmatched rows
from one or both tables.
The LEFT OUTER JOIN retrieves all the records from the left table and matching rows from the
right table. It will return NULL when no matching record is found in the right side table. Since
OUTER is an optional keyword, it is also known as LEFT JOIN.
The following syntax illustrates the use of LEFT OUTER JOIN in SQL Server:
SELECT column_lists
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
Lecture 26:
• Reusable: As mentioned, multiple users and applications can easily use and reuse
stored procedures by merely calling it.
• Easy to modify: You can quickly change the statements in a stored procedure as and
when you want to, with the help of the ALTER TABLE command.
• Low network traffic: The server only passes the procedure name instead of the whole
query, reducing network traffic.
• Increases performance: Upon the first use, a plan for the stored procedure is created
and stored in the buffer pool for quick execution for the next time.
Creating a stored procedure in SQL is as easy as it can get. The syntax of SQL stored procedure
is:
AS
BEGIN;
//statements;
END;
In the syntax mentioned above, the only thing to note here are the parameters, which can be
the following three types:
• IN: It is the default parameter that will receive input value from the program
• IN OUT: It is the combination of both IN and OUT. Thus, it receives from, as well as
sends a value to the program
Note: You will work with and look at examples for different parameters in this article.
You will use the syntax to create a simple stored procedure in SQL. But before that, create two
tables using the CREATE TABLE command that you will use throughout the article. You will also
insert some values in them using the INSERT INTO command.
CarID INT,
CarName VARCHAR(100)
);
Output:
CarID INT,
CarDescription VARCHAR(800)
);
INSERT INTO CarDescription VALUES (201,'Luxury motorcycle from the German automotive');
INSERT INTO CarDescription VALUES (301,'Luxury sports car from the Italian manufacturer');
INSERT INTO CarDescription VALUES (401,'Luxury SUV from the Italian automotive');
INSERT INTO CarDescription VALUES (501,'High-performance sports car from the German
manufacturer');
Output:
Now that you have created both the tables, start creating the stored procedure in SQL with the
syntax mentioned earlier. For the simple procedure, you will have to use the JOIN keyword to
join both the tables, and output a new one with CarID, CarName, and CarDescription.
AS
SET NOCOUNT ON
Car C
END
This will create the stored procedure, and you will see the “command(s) executed successfully”
message in Microsoft SQL Server Management Studio. Now, since you have created the
procedure, it's time to execute it. The syntax to execute the procedure is:
EXEC procedure_name
EXEC GetCarDesc;
Output:
As you can see in the output, the stored procedure executed the Join statement and gave the
desired result.
Lecture 27:
OOP Features
To create a class, you use the keyword "class" followed by the class name:
class Employee
{
}
C#
Copy
Object
1. The software is divided into several small units called objects. The data and
functions are built around these objects.
2. An object's data can be accessed only by the functions associated with that object.
3. The functions of one object can access the functions of another object.
Objects are the basic run-time entities of an object-oriented system. They may represent a
person, a place, or any item the program must handle.
A class will not occupy any memory space. Hence to work with the data represented by the
class, you must create a variable for the class, which is called an object.
• When an object is created using the new operator, memory is allocated for the
class in a heap, the object is called an instance, and its starting address will be
stored in the object in stack memory.
• When an object is created without the new operator, memory will not be allocated
in a heap; in other words, an instance will not be created, and the object in the
stack will contain the value null.
• When an object contains null, it is impossible to access the class members using
that object.
class Employee
{
}
C#
Copy
Copy
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Abstraction
Abstraction is "To represent the essential feature without representing the background details."
• Abstraction lets you focus on what the object does instead of how it does it.
• Abstraction provides a generalized view of your classes or objects by providing
relevant information.
• Abstraction is the process of hiding the working style of an object and showing the
information about an object understandably.
Lecture 28:
Real-world Example of Abstraction
Abstract information (necessary and common information) for the "Mobile Phone" object is
that it calls any number and can send an SMS.
So, for a mobile phone object, you will have the abstract class as in the following,
Copy
Abstraction means putting all the necessary variables and methods in a class.
Example
If some doctor gives you an application, you will provide the details, like name, address, date of
birth, blood group, height, and weight.
Age, name, and address, so you can create a class that consists of the common data. That is
called an abstract class.
That class is not complete, and other classes can inherit it.
Encapsulation
Wrapping up a data member and a method together into a single unit (in other words, class) is
called Encapsulation. Encapsulation is like enclosing in a capsule. That is, enclosing the related
operations and data related to an object into that object.
Encapsulation is like your bag in which you can keep your pen, book, etcetera. It means this is
the property of encapsulating members and functions.
class Bag {
book;
pen;
ReadBook();
}
C#
Copy
• Encapsulation means hiding the internal details of an object, in other words, how
an object does something.
• Encapsulation prevents clients from seeing its inside view, where the behavior of
the abstraction is implemented.
• Encapsulation is a technique used to protect the information in an object from
another object.
• Hide the data for security, such as making the variables private, and expose the
property to access the private data that will be public.
So, when you access the property, you can validate the data and set it.
Example 1
class Demo {
Copy
Suppose you are a Mobile Phone Manufacturer and have designed and developed a Mobile
Phone design (a class). Now by using machinery, you are manufacturing Mobile Phones
(objects) for selling; when you sell your Mobile Phone, the user only learns how to use the
Mobile Phone but not how the Mobile Phone works.
This means that you are creating the class with functions and by with objects (capsules), of
which you are making available the functionality of your class by that object and without
interference in the original class.
Example 2
TV operation
It is encapsulated with a cover, and we can operate it with a remote, and there is no need to
open the TV to change the channel. Everything is private except the remote, so anyone can
access the remote to operate and change the things on the TV.
Lecture 29:
Inheritance
Copy
Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Here the teacher is an object, but the attitude is different in different situations.
Example 2
A person behaves like a son in a house at the same time that the person behaves like an
employee in an office.
Example 3
• As phone
• As camera
• As mp3 player
• As radio
Real-world Example
Use an example of a Mobile Phone
You have a Mobile Phone; you can dial a number using keypad buttons. You don't even know
how these are working internally. This is called abstraction. You only have the information that
is necessary to dial a number. But the not internal working of the mobile. But how does the
Mobile Phone work internally? How are the keypad buttons connected to the internal circuit?
That is called Encapsulation.
Lecture 30:
Polymorphism means “Many Forms”. In polymorphism, poly means “Many,” and morph means
“Forms.” polymorphism is one of the main pillars in Object Oriented Programming. It allows you
to create multiple methods with the same name but different signatures in the same class. The
same name methods can also be in derived classes.
1. Method Overloading
2. Method Overriding
In this article, I will explain the method overloading and method overriding concept in C#.
Furthermore, I will try to demonstrate step-by-step differences between these.
Method Overloading
Method Overloading is a type of polymorphism. It has several names like “Compile Time
Polymorphism” or “Static Polymorphism,” and sometimes it is called “Early Binding”.
Method Overloading means creating multiple methods in a class with the same names but
different signatures (Parameters). It permits a class, struct, or interface to declare multiple
methods with the same name with unique signatures.
The compiler automatically calls the required method to check the number of parameters and
their type passed into that method.
using System;
namespace DemoCsharp
{
class Program
{
public int Add(int num1, int num2)
{
return (num1 + num2);
}
public int Add(int num1, int num2, int num3)
{
return (num1 + num2 + num3);
}
public float Add(float num1, float num2)
Copy
In the above example, you can see four methods with the same name, but the type of
parameters or several parameters is different. So when you call Add(4,5), the compiler
automatically calls the method, which has two integer parameters. On the other hand, when
you call Add(“hello”,”world”), the compiler calls the method, which has two string parameters.
So basically, in method overloading compiler checks, which method should be called at the
compilation time.
Note: Changing the method's return type does not overload the method. You cannot create a
method overloaded to vary only by return type.
Method Overriding
Method Overriding is a type of polymorphism. It has several names like “Run Time
Polymorphism” or “Dynamic Polymorphism,” and sometimes it is called “Late Binding”.
Method Overriding means having two methods with the same name and same signatures
[parameters]; one should be in the base class, and another method should be in a derived class
[child class]. You can override the functionality of a base class method to create the same name
method with the same signature in a derived class. You can achieve method overriding using
inheritance. Virtual and Override keywords are used to achieve method overriding.
Copy
then it adds successfully. But when you call the ChildClass Add method with less than zero
value, it checks for a negative value. If the passing values are negative, it asks for a new value.
So, here it is clear that we can modify the base class methods in derived classes.
Points to be remembered,