You are on page 1of 34

C-Sharp

C-Sharp
y Advanced Object Oriented y .NET platform y C# has roots in C, C++ and Java y IDE (Visual Studio)

Examples
using System; class Welcome {
static void Main( string[] args ) { Console.WriteLine( "Welcome to C# Programming!" ); }

Examples
using System; using System.Windows.Forms; class Welcome4 { static void Main( string[] args ) { MessageBox.Show( "Welcome\nto\nC#\nprogramming!" ); } }

Examples(Adding 2 numbers)
using System; class Addition { static void Main( string[] args ) { string firstNumber, // first string entered by user secondNumber; // second string entered by user int number1, // first number to add number2, // second number to add sum; // sum of number1 and number2 Console.Write( "Please enter the first integer: " ); firstNumber = Console.ReadLine();

Examples(Adding 2 numbers)
Console.Write( "\nPlease enter the second integer: " ); secondNumber = Console.ReadLine(); number1 = Int32.Parse( firstNumber ); number2 = Int32.Parse( secondNumber ); sum = number1 + number2; Console.WriteLine( "\nThe sum is {0}.", sum ); }// end method Main }// end class Addition

Control Structures
y goto elimination y 3 types of selection structures : y If(single-selection structure) y If/else(double-selection structure) y Switch Case(multiple-selection structure)

Control Structures
y 4 repetition structures y while, y do/while y for y foreach

Math Library
y Console.WriteLine( Math.Sqrt( 900.0 ) ); y If c1 = 13.0, d= 3.0 and f = 4.0, then the statement

Console.WriteLine( Math.Sqrt( c1 + d * f ) ); y Abs( x ) y Ceiling( x ) y Cos( x ) y Exp( x ) y Floor( x )

Math Library
y Log( x ) y Max( x, y ) y Min( x, y ) y Pow( x, y ) y Sin( x ) y Tan( x )

Arrays
y int[] c = new int[ 12 ]; y int[,] b = new int[ 2, 2 ]; y b[ 0, 0 ] = 1; y b[ 0, 1 ] = 2; y b[ 1, 0 ] = 3; y b[ 1, 1 ] = 4;

y int[,] b = { { 1, 2 }, { 3, 4 } };

Arrays
y int[][] array2 = new int[ 3 ][]; y array2[ 0 ] = new int[] { 1, 2 };
y array2[ 1 ] = new int[] { 3 }; y array2[ 2 ] = new int[] { 4, 5, 6 };

Lists (Add Values)


y List<int> list = new List<int>(); y list.Add(2); y list.Add(3); y list.Add(5); y list.Add(7);

Lists (Looping)
y List<int> list = new List<int>();

list.Add(2); list.Add(3); list.Add(7); foreach (int prime in list) { Console.WriteLine(prime); } for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); }

Lists (Clear List)


y List<int> list = new List<int>();

list.Add(2); list.Add(3); list.Add(7); Console.WriteLine(list.Count); // 3 list.Clear(); Console.WriteLine(list.Count); // 0

Copy array to list


y int[] arr = new int[3]; // New array with 3 elements

arr[0] = 2; arr[1] = 3; arr[2] = 5; List<int> list = new List<int>(arr); // Copy to List Console.WriteLine(list.Count); // 3 elements in List

Join string List


List<string> cities = new List<string>(); cities.Add("New York"); cities.Add("Mumbai"); cities.Add("Berlin"); cities.Add("Istanbul"); line string line = string.Join(",", cities.ToArray()); Console.WriteLine(line);

Insert
y List<int> numbers= new List<int>(); // Example List

numbers.Add(1); // Contains 1 numbers.Add(3)// Contains: 1, 3 numbers.Insert(1, 2)// Contains: 1, 2, 3

Data Base

What is a Database?
y A usually large collection of data organized especially

for rapid search and retrieval

Data Storage on Different Media

Electronic spreadsheet

Database Filing cabinet

Entity Relationship Model


y Create an entity relationship diagram from

business specifications or narratives:


EMPLOYEE #* number * name o job title assigned to com osed of DEPARTMENT #* number * name o location

y Scenario y . . . Assign one or more employees to a department . . . y . . . Some departments do not yet have assigned employees . . .

Entity Relationship Modeling Conventions

Entity
Singular, unique name Uppercase

Attribute
Singular name Lowercase Mandatory marked with * Optional marked with o

EMPLOYEE #* number * name o job title

assigned to composed of

DEPARTMENT #* number * name o location

Unique identifier (UID)


Primary marked with # Secondary marked with (#)

Query languages
y XQuery is a query language for XML data sources; y XPath is a declarative language for navigating XML

documents; y XSQL is a query language combining the power of XML and the power of SQL, it is currently under development

SQL SELECT Statement


y Used to query or retrieve data from the database.

Syntax of SQL SELECT Statement: SELECT column_list FROM table-name


 table-name is the name of the table from which the

information is retrieved.  column_list includes one or more columns from which data is retrieved.  The code within the brackets is optional.

SQL SELECT Statement


Example : y Table Name: Student Details
id 100 101 102 103 104 first_name Rahul Anjali Stephen Shekar Priya last_name Sharma Bhagwat Fleming Gowda Chandra age 10 12 09 18 15 subject Science Maths Science Maths Economics games Cricket Football Cricket Badminton Chess

y SELECT first_name FROM student_details;

OR y SELECT first_name, last_name FROM student_details;

SQL WHERE Clause


Syntax of SQL WHERE Clause: WHERE {column or expression} comparison-operator value;  column or expression - is the column of a table or a expression  comparison-operator - operators like = < > etc.  value - Any user value or a column name for comparison Examples: SELECT first_name, last_name FROM student_details WHERE id = 100; SELECT name, salary, salary*1.2 AS new_salary FROM employee WHERE salary*1.2 > 30000; y Output: name salary new_salary
----------Hrit i Hars a ri a ---------3 3 3 ---------------37 37 36

SQL Comparison Keywords


SQL LIKE Operator: y Is used to list all rows in a table whose column values match a specified pattern.
y Example:

SELECT first_name, last_name FROM student_details WHERE first_name LIKE 'S%';


first_name ------------Step e She ar last_name -------------

id

first_name last_name ahul A jali Shar a Bhagwat Fle i g Gowda ha dra

age

subject Scie ce

games ricket Football ricket

2 9 8

Maths Scie ce

2 3

Stephe Shekar ri a

Maths Bad i to co o ics hess

Fle i g Gowda

% :Substitutes with any number of characters. _ :signifies a single character

SQL Comparison Keywords


SQL BETWEEN ... AND Operator: y Is used to compare data for a range of values.
y Example:

SELECT first_name, last_name, age FROM student_details WHERE age BETWEEN 10 AND 15;
first_name ------------Rahul A ajali Shekar last_name ------------Shar a Bhagwat Gowda 2 age ------

id

first_name last_name Rahul A jali Shar a Bhagwat Fle i g Gowda ha dra

age

subject Scie ce

games ricket Football ricket

2 9 8

Maths Scie ce

2 3

Stephe Shekar ri a

Maths Bad i to co o ics hess

SQL Comparison Keywords


SQL IN Operator: y Is used when you want to compare a column with more than one value. It is similar to an OR condition.
y Example:

SELECT first_name, last_name, subject FROM student_details WHERE subject IN ('Maths', 'Science');
first_name ------------A ajali Shekar Rahul Stephe last_name ------------Bhagwat Gowda Shar a Fle i g subject ---------Maths Maths Scie ce Scie ce

id

first_name last_name Rahul A jali Shar a Bhagwat Fle i g Gowda ha dra

age

subject Scie ce

games ricket Football ricket

2 9 8

Maths Scie ce

2 3

Stephe Shekar ri a

Maths Bad i to co o ics hess

SQL ORDER BY
in ascending order by default. ORDER BY Syntax : y SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1 [, column2, .. columnN] [DESC]];
y Example:
 SELECT name, salary FROM employee ORDER BY salary;  SELECT name, salary FROM employee ORDER BY name, salary;

y is used to sort results either in ascending or descending order. Oracle sorts query results

The same as : SELECT name, salary FROM employee ORDER BY 1, 2;


 SELECT name, salary FROM employee ORDER BY name, salary DESC;  SELECT name, salary, salary*1.2 AS new_salary FROM employee WHERE salary*1.2

> 30000 ORDER BY new_salary DESC;

SQL GROUP Functions


y are built-in SQL functions that operate on groups of rows and return one value for the

entire group. These functions are: COUNT, MAX, MIN, AVG, SUM. SQL COUNT (): y This function returns the number of rows in the table that satisfies the condition specified in the WHERE condition

y Example:
 SELECT COUNT (*) FROM employee WHERE dept = 'Electronics';  SELECT COUNT (*) FROM employee;

SQL GROUP Functions


SQL MAX(): This function is used to get the maximum value from a column. y Example:  SELECT MAX (salary) FROM employee; SQL MIN(): This function is used to get the minimum value from a column. y Example:  SELECT MIN(salary) FROM employee;

SQL GROUP Functions


SQL AVG(): This function is used to get the average value of a numeric column. Example:  SELECT AVG (salary) FROM employee; SQL SUM(): This function is used to get the sum of a numeric column. y Example:  SELECT SUM (salary) FROM employee;

You might also like