Chapter 6
Chapter 6
Chapter 6
30-12-2016
Outline
o LINQ Architecture
30-12-2016
Database Concepts
o A database is a collection of one or more tables, each
containing data related to a particular topic.
o Visual Basic applications use database management
systems to make large amounts of data available to
programs.
o Visual Basic can interact with many DBMS’s
– Oracle
– MySQL
o Microsoft SQL Server 2008 Express used in this chapter
30-12-2016
Layered Approach to Using a DBMS
30-12-2016
Terminology
30-12-2016
Database Table
30-12-2016
VB and SQL Server Data Types
30-12-2016
One-to-Many Relationships
30-12-2016
What is ADO.NET?
30-12-2016
o What is the difference between ADO and ADO.Net
• ADO works with the connected data whereas ADO.Net works in
a disconnected manner.
• ADO has main object called Recordset which is used to
reference data. But ADO.Net has various objects to access the
database
30-12-2016
ADO.NET Architecture
30-12-2016
LINQ Architecture
o LINQ that stands for Language Integrated Query
(pronounced as “link”) is a .NET language extension that
supports data retrieval from different data sources like
XML document, databases and collections.
o It was introduced in the .NET 3.5 framework.
o VB and C# are the languages that have LINQ capabilities.
o Introduced in Visual Studio 2008 and designed by Anders
Hejlsberg.
• Allows writing queries even without the knowledge of
query languages like SQL, XML etc.
• LINQ queries can be written for diverse data types.
30-12-2016
LINQ Architecture…
o Example of a LINQ query
Module Module1
Sub Main()
Dim words As String() = {"hello", "wonderful", "LINQ", "beautiful", "world"}
' Get only short words
Dim shortWords = From word In words
Where word.Length <= 5
Select word
' Print each word out. Output
For Each word In shortWords hello
Console.WriteLine(word) LINQ
Next world
Console.ReadLine()
End Sub
End Module
30-12-2016
LINQ Architecture…
Syntax of LINQ
oThere are two syntaxes of LINQ. These are the following ones.
1.Lamda Method Syntax
oExample
var longWords = words.Where( w => w.length > 10);
Dim longWords = words.Where(Function(w) w.length > 10)
2.Query Comprehension Syntax
oExample
var longwords = from w in words where w.length > 10;
Dim longwords = from w in words where w.length > 10
30-12-2016
Types of LINQ
o The types of LINQ are mentioned below in brief.
1. LINQ to Objects:
o Allows querying in-memory objects like arrays, lists, generic list and any type of
collections.
2. LINQ to XMLXLINQ:
o Allows querying the XML document by converting the document into XElement
objects and then querying using the local execution engine.
3. LINQ to DataSet:
o Allows query to any database that can be queried with ADO.NET.
4.LINQ to SQL DLINQ:
o It’s specifically used to work with the SQL server database
5. LINQ to Entities:
o It is similar to LINQ to SQL. It allows developers to query the conceptual entity
data model
30-12-2016
LINQ Architecture in .NET
o LINQ has a 3-layered architecture in which the uppermost layer
consists of the language extensions and the bottom layer consists of
data sources.
30-12-2016
LINQ Architecture in .NET…
30-12-2016
LINQ Architecture in .NET…
SqlConnection.Open();
return sqlCommand.ExecuteReader
(CommandBehavior.CloseConnection)
30-12-2016
LINQ Architecture in .NET…
select c;
30-12-2016
Advantages of LINQ
30-12-2016
Advantages of LINQ…
o LINQ allows usage of a single LINQ syntax while querying many diverse
data.
o LINQ is extensible that means it is possible to use knowledge of LINQ to
querying new data source types.
o LINQ offers the facility of joining several data sources in a single query
as well as breaking complex problems into a set of short queries easy to
debug.
o LINQ offers easy transformation for conversion of one data type to
another like transforming SQL data to XML data.
30-12-2016
The .NET Data providers
o Oracle
30-12-2016
The .NET Data providers…
– Connection
– Command
– DataReader
– DataAdapter
30-12-2016
Working with the common .NET Data providers
Oracle System.Data.OracleClient
30-12-2016
Working with the common .NET Data providers
Object Description
Connection Allows establishing and releasing connections, and to begin
transactions
Command Executes a command against a data source. Often in the form of SQL
statements that retrieves data from the data source.
Data Reader Reads a forward-only, read-only stream of data from a data source
Data Adapter Populates a dataset and updates a database.
30-12-2016
Command Methods
30-12-2016
The Dataset Component
o In-memory representation of data contained in a
database/XML
o Operations are performed on the DataSet, not the data source
o Can be created programmatically, using a DataAdapter or
XML schema and document (or any mixture)
Creating DataSets
Setup SqlConnection
Setup a SqlDataAdapter
Create a DataSet
30-12-2016
DataReader Vs. DataSet
Datareader Dataset
30-12-2016
DataAdapters
sqlDA.Fill(sqlDS, "authorsTable");
30-12-2016
DataTables
30-12-2016
DataRelations
o New to ADO.Net
DataViews
o Like a SQL view
o Single, or multiple tables
o Normally used with GUI applications via Data Binding.
30-12-2016
Using the DataGridView for database access
30-12-2016
How to connect VB.Net to SQL Server
o Here is the step by step procedure to connect vb.net to SQL server.
1. Create your VB.NET project
2. Include the following namespace
Imports System.Data
Imports System.Data.SqlClient
o The System.Data namespace provides access to classes that represent
the ADO.Net architecture.
o The System.Data.SqlClient namespace is the .Net framework data
provider for Sql server.
3. Declare and instantiate your Sql connection object as shown below
Dim con As New SqlConnection
30-12-2016
How to connect VB.Net to SQL Server…
30-12-2016
How to insert data to SQL Server…
Try
con.ConnectionString = ("Data Source=(local);Initial Catalog=registrar;Integrated Security=True")
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into table([field1],[field2])VALUES([Value1],[Value2]) "
cmd.ExecuteNonQuery()
MsgBox("data is successfully inserted into database")
Catch ex As Exception
MessageBox.Show("the value is not properly entered into the database" & ex.Message, "insert")
Finally
con.Close()
End Try
End Try
30-12-2016
How to connect VB.Net to SQL Server…
30-12-2016
How to connect VB.Net to SQL Server…
o How to update record on SQL database
Dim con As SqlConnection
Dim cmd As New SqlCommand
Try
Dim con As SqlConnection = New SqlConnection("Data Source=(local);Initial
Catalog=registrar;Integrated Security=True")
con.Open()
cmd.Connection=con
cmd.CommandText=“UPDATE table SET field1=Value1,field2=Value2 where field3=‘Test’”
cmd.ExecuteNonQuery()
MessageBox.Show("This record is updated succesfully")
Catch ex As ExceptionessageBox.Show(“Error while Updating record on table…” &ex.Message
“update Records”)
Finally
con.Close()
End Try
30-12-2016
How to connect VB.Net to SQL Server…
30-12-2016
Thank You
End of Class
December 30,2016
30-12-2016