NIT Inbuilt Functions and Database Access Using ADO ET
NIT Inbuilt Functions and Database Access Using ADO ET
3
5.1 INBUILT FUNCTIONS
1) Mathematical Functions
2) String Manipulation Functions
3) Formatting Functions
4
1. MATHEMATICAL FUNCTIONS
All the Math functions of VB have been replaced
by the methods of the Math class.
1) Abs Function
This method returns the absolute value of its argument.
Example: Math.Abs(-8)
5
2) Exp function
The Exp of a number x is the exponential value of x,
i.e. ex .
3) Fix Function
The Fix function truncate the decimal part of a positive
number and returns the largest integer but if
negative number give it returns smaller than the
number.
Syntax: Fix(Number) 6
Example: Fix(9.7)
4) Int Function
The Int is a function that converts a number into an
integer by truncating its decimal part.
Syntax: Int(Number)
Example: Int(2.4
5) Log Function
The Log function is the function that returns the natural
logarithm of a number.
Syntax: Math.Log (Number)
Example: Math.Log(10)
7
6) Rnd( ) Function
The Rnd function returns a random value between
0 and 1.
Syntax: Rnd(Number)
Number is an optional argument
7) Round Function
The Round function is the function that rounds up a
number to a certain number of decimal places.
Syntax: Math.Round(Number)
9
5.2. STRING MANIPULATION FUNCTIONS
1) Mid(string, start, [length]) Function
The Mid() function returns a section of a string of
length characters, starting at position start.
Syntax: Mid(string, start, [length])
Example: Mid(“Good Morning”,1,4)
10
3) Left(string, number)Function
This function returns a number of characters
from the left side(beginning) of a string.
Example: Left(“Morning”,1)
4) Trim(“String”)Function
Syntax: Trim(“String”)
Syntax: InStr([start,],string1,string2,[compare])
Example: InStr(1,”Good Night”,
”Good”,CompareMethod.Text)
13
8) Ucase Function
This function accepts a string or character as an
argument and returns a string or character converted
into upper case.
Syntax : UCase(“String” or Character)
Example: UCase(“vpmp”)
9) Lcase Function
This function accepts a string or character as an
argument and returns a string or character converted
into lowercase.
Syntax : LCase(“String” or Character)
Example: LCase(“VPMP”)
14
9) Chr Function
This function returns a character corresponds to the
ASCII value specified as an argument.
Syntax : Chr(char code)
Example: Chr(65)
Example: Asc(“A”)
15
3. FORMATTING FUNCTION
1. Now() Function
This function returns both the system date and time.
The statement
Syntax:
MsgBox(Now())
displays a date/time combination such as
9/13/1998 09:23:10 PM in a message box. To extract the
date or time part of the value returned by the Now()
function, use the Date and TimeOfDay
properties:
Console.WriteLine(Now.Date.ToString)
Console.WriteLine(Now.TimeOfDay.ToString) 16
Formate Styles
Formate Style Description
General Date Format the Current Date & Time
Long Date or D Display the Current Date in long format
Short Date or d Display the Current Date in Short format
17
Example
Example Output
Format(Date.Now,”Gener 28/3/2017 3:10 PM
al Date”)
Format(Date.Now, “D”) Monday , March 28 2017
Format(Date.Now, “d”) 28/3/2017
18
5.3 ADO.NET OBJECT MODEL
ADO.NET stands for Active X Data Object.
ADO.NET is an Object Oriented Set of Libraries
that allows to interact with Data Sources.& Data
Source is a Database.
ADO.NET provides data access services in the Microsoft
.NET platform.
19
ADO.NET OBJECT MODEL /
ADO.NET ARCHITECTURE
20
ADO.NET OBJECT MODEL
You can use ADO.NET to access data by using the new
.NET Framework data providers which are:
21
ADO.NET OBJECT MODEL
Architecture
of ADO.net basically consist of
two Components
22
1.1 CONNECTION OBJECT
The Connection object Create the connection to the
database.
The ADO Connection Object is used to create an open
connection to a data source. Through this connection,
you can access and manipulate a database.
It is Two type of connection provide.
1. SqlConnection
2. OleDbConnection
1) Microsoft Access
Dim cn As New SqlConnection()
or 23
Connection String
Connection string is a sequence of name and
value pair separated by semicolon.
Connection string is used to specify name of provider,
type of data source , its location , username, password
24
etc.
EXAMPLE OF CONNECTION STRING
cn.ConnectionString=
"DataSource=.\SQLEXPRESS;AttachDbFilename
=C:\Users\-\documents\visual studio
2010\Projects\WindowsApplication1\Windows
Application1\Database1.mdf;Integrated
Security=True;User Instance=True"
25
PROPERTIES OF CONNECTION OBJECT
26
METHODS OF CONNECTION OBJECT
27
2. COMMAND OBJECT
The ADO Command object is used specify query to
perform with data source or database.
28
.Net Provides Two Type of Command
Classes:
1. SqlCommand
2. Oledb Command
29
COMMAND OBJECT
Syntax to create Command Object
1) For Microsoft Access
Dim cmd As New OleDbCommand()
Or
Dim cmd As New OleDbCommand(Query, connection)
30
Example-1
Dim cn As New SqlConnection
("DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\-
\documents\visual studio
2010\Projects\WindowsApplication1\WindowsApplication1\Database
1.mdf;Integrated Security=True;User Instance=True“)
31
3. DATA READER
DataReader provides a Read-only, Forward-only
stream of result from a database.
Data reader object is used to retrieve records from data
source and examine them one by one.
The important property of data reader are
1) Fast
2) Forward only
3) Read only
Syntax
Dim dr As OleDbDataReader()
or
Dim dr As SqlDataReader()
33
4. DATA ADAPTER
A DataAdapter (DataAdapter class) is an object used to
exchange data between a data source and a data set.
34
Properties of DataAdapter
Properties Description
35
Create Data Adapter Object
Syntax
Syntax
36
Example-1
Dim cn As New SqlConnection(“Connection String”)
Dim q1 As String = “ select * from student;”
Dim cm As New SqlCommand(q1,cn)
Dim da As New SqlDataAdapter(cm)
37
DIFFERENCES BETWEEN DATA ADAPTER &
DATA READER
Data Reader DataAdapter
Data Reader provides open & DataAdapter provides a
static forward–only connection. disconnected data
architechecture.
Single Row can be read at a Multiple Rows can be read at a
Time. Time.
Provides record in read-only Provides property to
mode. insert,update delete& retrieve
rows using commands.
Good choice for retrieving large Good choice for retrieving as
amount of data. well as to insert,update delete
data. 38
It works quickly It is not works quickly
2. DATASET
The DataSet is a Disconnected storage &
disconnected in Memory representation of Data.
40
DATASET
41
PROPERTY OF DATASET
42
METHODS OF DATASET
43
DATA TABLE
DataTable is a collection of DataColumn, DataRow
and Constrain. The concept of DataTable is similar
to the concept of tables in DataBase.
DataTable object allows you to examine data
through collection of rows and columns.
You can store the result of a query in a DataTable
through DataAdapter objects fill method.
Create DataTable object
Syntax: Dim dt as new DataTable ()
Or
Dim dt as new DataTable (“TableName”)
44
DATACOLUMN COLLECTION
Each DataTable has a column collection
The concept of DataColumn is similar to the concept
of column in the DataBase table.
DataColumn doesn’t actually contain data but it
contain the information about the structure of the
column.
Create DataColumn
Syntax: Dim col as new DataColumn()
Or
Dim col as new DataColumn(“ColumnName”)
45
DATA ROWS COLLECTION
The DataRow and DataColumn objects are
primary components of a DataTable.
Use the DataRow object and its properties and
methods to retrieve and evaluate; and insert,
delete, and update the values in the DataTable.
The DataRowCollection represents the actual
DataRow objects in the DataTable, and the
DataColumnCollection contains the DataColumn
objects that describe the schema of the
DataTable. Use the overloaded Item property to
return or set the value of a DataColumn.
46
DATA RELATION OBJECT
DataRelation object allows user to specify
relationship between different columns of different
DataTable objects.
Relationship is useful to provide data integrity and
consistency. Relationship can be established
between two tables by defining a primary key in one
table and using a foreign key in the other table.
Create Data Relation object
Syntax:
Dim dr as new DataRelation(“Relation
Name”, ”ParentColumn”, ”ChildColumn”)
47
5.4 Bounded Gridview Step
Step 1 : Go to Data Sources and click at "Add New Data
Source".
Step 2 : A new window will be open.
Step 3 : Click the Next button.
Step 4 : Click the Next button.
Step 5 : Click the "New connection" button to make a
new database connection.
48
Step 6 : A new window will open asking to
save the connection string.
Step 7 : Click the Next button. In the new
window click the Table node to explore all
the tables of your database.
Step 8 : Click the table node to explore all
columns. Then check the checkbox for each
column to select them. Look at the
following figure.
Step 9 : Click the finish button.
49
GTU IMP QUESTION
1) Draw and Explain ADO.net Architecture .
2) Explain DataAdapter object with Example.
3) Explain DataTable, DataRow, and DataColumn
4) Explain DataConnection with Example.
5) Explain CommandObject with Example.
6) Explain (i) Abs (ii) Sqrt (iii)Int() (iv) Rnd()
7) Explain (i) Exp (ii) MAX (iii)Ceil (iv) Pow
8) Explain DataSet in brief.
50
51
52