SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
 
Telemark	
  University	
  College	
  
Department	
  of	
  Electrical	
  Engineering,	
  Information	
  Technology	
  and	
  Cybernetics	
  
Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
	
  
	
  
	
  
	
  
Using	
  SQL	
  Server	
  in	
  C#	
  
with	
  Examples	
  
Hans-­‐Petter	
  Halvorsen,	
  2014.03.01	
  
	
  
	
  
 
2	
  
	
  
	
  
Table	
  of	
  Contents	
  
1.	
   Introduction	
  .......................................................................................................................	
  4	
  
2.	
   SQL	
  Server	
  ..........................................................................................................................	
  5	
  
2.1.	
   Database	
  Systems	
  .......................................................................................................	
  5	
  
2.2.	
   Structured	
  Query	
  Language	
  ........................................................................................	
  5	
  
3.	
   Visual	
  Studio	
  ......................................................................................................................	
  6	
  
3.1.	
   The	
  .NET	
  Framework	
  ...................................................................................................	
  6	
  
3.2.	
   The	
  C#	
  Language	
  .........................................................................................................	
  6	
  
4.	
   Database	
  Programming	
  in	
  C#	
  .............................................................................................	
  7	
  
4.1.	
   ADO.NET	
  .....................................................................................................................	
  7	
  
4.2.	
   Enterprise	
  Library	
  &	
  Data	
  Access	
  Application	
  Block	
  ...................................................	
  8	
  
4.3.	
   LINQ	
  ............................................................................................................................	
  8	
  
4.3.1.	
   LINQ	
  to	
  SQL	
  ..........................................................................................................	
  8	
  
4.4.	
   Entity	
  Framework	
  ........................................................................................................	
  8	
  
4.4.1.	
   LINQ	
  to	
  Entities	
  ....................................................................................................	
  8	
  
5.	
   ADO.NET	
  ..........................................................................................................................	
  10	
  
5.1.	
   Introduction	
  ..............................................................................................................	
  10	
  
5.2.	
   Examples	
  ...................................................................................................................	
  10	
  
6.	
   Enterprise	
  Library	
  .............................................................................................................	
  11	
  
6.1.	
   Data	
  Access	
  Application	
  Block	
  ..................................................................................	
  11	
  
6.1.1.	
   Installation	
  .........................................................................................................	
  11	
  
7.	
   LINQ	
  .................................................................................................................................	
  13	
  
3	
   	
   Table	
  of	
  Contents	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
7.1.	
   LINQ	
  to	
  SQL	
  ...............................................................................................................	
  13	
  
8.	
   Entity	
  Framework	
  .............................................................................................................	
  14	
  
8.1.	
   …	
  ...............................................................................................................................	
  14	
  
9.	
   Web	
  Services	
  ....................................................................................................................	
  15	
  
10.	
   3-­‐Tier	
  Architecure	
  ..........................................................................................................	
  18	
  
11.	
   References	
  .....................................................................................................................	
  19	
  
	
  
 
4	
  
	
  
1.	
   Introduction	
  
This	
  Tutorial	
  gives	
  an	
  overview	
  of	
  different	
  techniques	
  for	
  communicationg	
  with	
  SQL	
  Server	
  
in	
  Visual	
  Studio	
  and	
  C#.	
  
For	
  more	
  information	
  about	
  Software	
  and	
  Programming,	
  please	
  see	
  my	
  web	
  site	
  located	
  
here:	
  
http://home.hit.no/~hansha/	
   	
   	
   	
  
	
  
	
  
 
5	
  
	
  
2.	
   SQL	
  Server	
  
…	
  
2.1.	
   Database	
  Systems	
  
…	
  
For	
  more	
  information	
  about	
  Database	
  Systems,	
  please	
  see	
  the	
  Tutorial	
  “Introduction	
  to	
  
Database	
  Systems”	
  located	
  here:	
  
http://home.hit.no/~hansha/?tutorial=database	
   	
  
2.2.	
   Structured	
  Query	
  Language	
  
…	
  
For	
  more	
  information	
  about	
  SQL,	
  please	
  see	
  the	
  Tutorial	
  “Structured	
  Query	
  Language”	
  
located	
  here:	
  
http://home.hit.no/~hansha/?tutorial=sql	
   	
  
	
  
	
  
	
  
 
6	
  
	
  
3.	
   Visual	
  Studio	
  
Microsoft	
  Visual	
  Studio	
  is	
  an	
  integrated	
  development	
  environment	
  (IDE)	
  from	
  Microsoft.	
  
Home	
  page	
  of	
  Visual	
  Studio:	
  http://www.visualstudio.com	
   	
  
For	
  more	
  information	
  about	
  Visual	
  Studio	
  and	
  C#,	
  please	
  see	
  the	
  Tutorial	
  “Introduction	
  to	
  
Visual	
  Studio	
  and	
  C#”	
  located	
  here:	
  
http://home.hit.no/~hansha/?tutorial=csharp	
  
3.1.	
   The	
  .NET	
  Framework	
  
The	
  .NET	
  Framework	
  (pronounced	
  “dot	
  net”)	
  is	
  a	
  software	
  framework	
  that	
  runs	
  primarily	
  on	
  
Microsoft	
  Windows.	
  It	
  includes	
  a	
  large	
  library	
  and	
  supports	
  several	
  programming	
  languages	
  
such	
  as	
  C#.	
  .NET	
  is	
  included	
  with	
  Visual	
  Studio.	
  
3.2.	
   The	
  C#	
  Language	
  
C#	
  is	
  pronounced	
  “see	
  sharp”.	
  C#	
  is	
  an	
  object-­‐oriented	
  programming	
  language	
  and	
  part	
  of	
  
the	
  .NET	
  family	
  from	
  Microsoft.	
  C#	
  is	
  very	
  similar	
  to	
  C++	
  and	
  Java.	
  C#	
  is	
  developed	
  by	
  
Microsoft	
  and	
  works	
  only	
  on	
  the	
  Windows	
  platform.	
  
	
   	
  
	
  
	
  
	
  
	
  
 
7	
  
	
  
4.	
   Database	
  Programming	
  
in	
  C#	
  
Visual	
  Studio,	
  .NET	
  and	
  C#	
  offers	
  lots	
  of	
  different	
  techniques	
  for	
  communicationg	
  with	
  
Databases,	
  espessially	
  SQL	
  Server.	
  
Some	
  of	
  the	
  techniques	
  discussed	
  in	
  this	
  document	
  are:	
  
• ADO.NET	
  
• Enterprise	
  Library	
  &	
  Data	
  Access	
  Application	
  Block	
  
• LINQ	
  
o LINQ	
  to	
  SQL	
  
o LINQ	
  to	
  Enitities	
  
• Entity	
  Framework	
  
4.1.	
   ADO.NET	
  
ADO.NET	
  is	
  the	
  basic	
  data-­‐access	
  technology	
  for	
  .NET.	
  
ADO.NET	
  (ActiveX	
  Data	
  Object	
  for	
  .NET)	
  is	
  a	
  set	
  of	
  computer	
  software	
  components	
  that	
  
programmers	
  can	
  use	
  to	
  access	
  data	
  and	
  data	
  services.	
  It	
  is	
  a	
  part	
  of	
  the	
  base	
  class	
  library	
  
that	
  is	
  included	
  with	
  the	
  Microsoft	
  .NET	
  Framework.	
  It	
  is	
  commonly	
  used	
  by	
  programmers	
  to	
  
access	
  and	
  modify	
  data	
  stored	
  in	
  relational	
  database	
  systems,	
  though	
  it	
  can	
  also	
  access	
  data	
  
in	
  non-­‐relational	
  sources.	
  
http://msdn.microsoft.com/en-­‐us/library/e80y5yhx(v=vs.110).aspx	
  
ADO.NET	
  provides	
  consistent	
  access	
  to	
  data	
  sources	
  such	
  as	
  SQL	
  Server	
  and	
  XML,	
  and	
  to	
  
data	
  sources	
  exposed	
  through	
  OLE	
  DB	
  and	
  ODBC.	
  
The	
  ADO.NET	
  classes	
  are	
  found	
  in	
  System.Data.dll.	
   	
  
ADO.NET	
  Code	
  Examples:	
  
http://msdn.microsoft.com/en-­‐us/library/dw70f090(v=vs.110).aspx	
   	
  
8	
   	
   Error!	
  Reference	
  source	
  not	
  found.	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
4.2.	
   Enterprise	
  Library	
  &	
  Data	
  Access	
  
Application	
  Block	
  
Microsoft	
  Enterprise	
  Library	
  is	
  a	
  collection	
  of	
  different	
  libraries	
  that	
  makes	
  your	
  life	
  as	
  a	
  
programmer	
  easier.	
  In	
  this	
  document	
  we	
  will	
  only	
  use	
  the	
  Data	
  Access	
  Application	
  Block.	
  
The	
  Data	
  Access	
  Application	
  Block	
  simplifies	
  many	
  common	
  data	
  access	
  tasks	
  such	
  as	
  
reading	
  data	
  for	
  display,	
  passing	
  data	
  through	
  application	
  layers,	
  and	
  submitting	
  changed	
  
data	
  back	
  to	
  the	
  database	
  system.	
  It	
  includes	
  support	
  for	
  both	
  stored	
  procedures	
  and	
  in-­‐line	
  
SQL,	
  can	
  expose	
  the	
  data	
  as	
  a	
  sequence	
  of	
  objects	
  for	
  client-­‐side	
  querying,	
  and	
  provides	
  
access	
  to	
  the	
  most	
  frequently	
  used	
  features	
  of	
  ADO.NET	
  in	
  simple-­‐to-­‐use	
  classes.	
  
You	
  can	
  use	
  the	
  NuGet	
  Package	
  Manager	
  in	
  Visual	
  Studio	
  to	
  install	
  the	
  Enterprise	
  Library	
  
assemblies	
  that	
  you	
  need	
  in	
  your	
  projects.	
  
Data	
  Access	
  Application	
  Block	
  Web	
  Site:	
  
http://msdn.microsoft.com/en-­‐us/library/dn440726(v=pandp.60).aspx	
   	
  
4.3.	
   LINQ	
  
…	
  
4.3.1.	
   LINQ	
  to	
  SQL	
  
LINQ	
  to	
  SQL	
  is	
  a	
  method	
  to	
  work	
  with	
  Microsoft	
  SQL	
  Server	
  databases	
  using	
  LINQ.	
   	
  
Microsoft	
  recommends	
  that	
  you	
  use	
  Entity	
  Framework	
  for	
  new	
  applications,	
  because	
  LINQ	
  
to	
  SQL	
  will	
  not	
  be	
  further	
  developed.	
  
4.4.	
   Entity	
  Framework	
  
…	
  
Entity	
  Framework	
  Overview:	
  
http://msdn.microsoft.com/en-­‐us/library/bb399567(v=vs.110).aspx	
   	
  
4.4.1.	
   LINQ	
  to	
  Entities	
  
9	
   	
   Error!	
  Reference	
  source	
  not	
  found.	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
…	
  
	
  
	
  
 
10	
  
	
  
5.	
   ADO.NET	
  
…	
  
5.1.	
   Introduction	
  
…	
  
5.2.	
   Examples	
  
…	
  
	
  
 
11	
  
	
  
6.	
   Enterprise	
  Library	
  
Enterprise	
  Library	
  is	
  part	
  of	
  Microsoft	
  Patters	
  &	
  Practices.	
  Microsoft	
  Patters	
  &	
  Practices	
  are	
  
recommendations	
  on	
  how	
  to	
  design	
  and	
  develop	
  custom	
  applications	
  using	
  the	
  Microsoft	
  
platform.	
  
	
  
Microsoft	
  Patters	
  &	
  Practices	
  Web	
  Site:	
  
http://msdn.microsoft.com/en-­‐us/library/ff921345.aspx	
   	
  
Enterprise	
  Library	
  Web	
  Site:	
  http://msdn.microsoft.com/en-­‐us/library/ff648951.aspx	
   	
  
Microsoft	
  Enterprise	
  Library	
  is	
  a	
  collection	
  of	
  different	
  libraries	
  that	
  makes	
  your	
  life	
  as	
  a	
  
programmer	
  easier.	
  In	
  this	
  document	
  we	
  will	
  only	
  use	
  the	
  Data	
  Access	
  Application	
  Block.	
  
6.1.	
   Data	
  Access	
  Application	
  Block	
  
The	
  Data	
  Access	
  Application	
  Block	
  simplifies	
  many	
  common	
  data	
  access	
  tasks	
  such	
  as	
  
reading	
  data	
  for	
  display,	
  passing	
  data	
  through	
  application	
  layers,	
  and	
  submitting	
  changed	
  
data	
  back	
  to	
  the	
  database	
  system.	
  It	
  includes	
  support	
  for	
  both	
  stored	
  procedures	
  and	
  in-­‐line	
  
SQL,	
  can	
  expose	
  the	
  data	
  as	
  a	
  sequence	
  of	
  objects	
  for	
  client-­‐side	
  querying,	
  and	
  provides	
  
access	
  to	
  the	
  most	
  frequently	
  used	
  features	
  of	
  ADO.NET	
  in	
  simple-­‐to-­‐use	
  classes.	
  
6.1.1.	
   Installation	
  
You	
  can	
  use	
  the	
  NuGet	
  Package	
  Manager	
  in	
  Visual	
  Studio	
  to	
  install	
  the	
  Enterprise	
  Library	
  
assemblies	
  that	
  you	
  need	
  in	
  your	
  projects.	
  
Installation	
  of	
  the	
  The	
  Data	
  Access	
  Application	
  Block:	
  
12	
   	
   Error!	
  Reference	
  source	
  not	
  found.	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
	
  
	
  
	
  
 
13	
  
	
  
7.	
   LINQ	
  
…	
  
7.1.	
   LINQ	
  to	
  SQL	
  
…	
  
 
14	
  
	
  
8.	
   Entity	
  Framework	
  
…	
  
8.1.	
   …	
  
…	
  
	
  
	
  
	
  
	
  
 
15	
  
	
  
9.	
   Web	
  Services	
  
In	
  many	
  situations	
  our	
  apps	
  don’t	
  necessarly	
  have	
  direct	
  access	
  to	
  the	
  datbase,	
  especially	
  
when	
  your	
  sofware	
  is	
  used	
  ourside	
  the	
  local	
  company	
  network.	
  In	
  these	
  situations	
  we	
  need	
  
to	
  use	
  a	
  Web	
  Service	
  in	
  order	
  to	
  write	
  or	
  read	
  data	
  from	
  the	
  database.	
  
	
  
Today	
  Web	
  Services	
  have	
  been	
  very	
  popular.	
  A	
  Web	
  service	
  is	
  a	
  method	
  of	
  communications	
  
between	
  two	
  devices	
  over	
  the	
  World	
  Wide	
  Web,	
  and	
  makes	
  it	
  easy	
  to	
  share	
  data	
  over	
  a	
  
network	
  or	
  the	
  internet.	
  
A	
  Web	
  Service	
  is	
  
• A	
  Web	
  API	
  
• A	
  Standard	
  defined	
  by	
  W3C	
  
• Cross-­‐platform	
  and	
  Platform-­‐independent	
  Communication	
  
• Distributed	
  Application	
  Development	
   	
  
Web	
  Services	
  can	
  be	
  implemented	
  and	
  used	
  in	
  most	
  Programming	
  Languages	
  (C#/ASP.NET,	
  
PHP,	
  LabVIEW,	
  Objective-­‐C,	
  Java,	
  etc.)	
  
16	
   	
   Error!	
  Reference	
  source	
  not	
  found.	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
Web	
  Services	
  uses	
  standard	
  Web	
  technology	
  (Web	
  protocols)	
  such	
  as	
  HTTP,	
  REST,	
  SOAP,	
  
XML,	
  WSDL,	
  JSON,	
  etc.	
  
	
  
Web	
  Services	
  technology	
  used	
  in	
  Web	
  Services:	
  
• HTTP	
  -­‐	
  Hypertext	
  Transfer	
  Protocol	
  
• XML	
  –	
  Extensible	
  Markup	
  Language	
  
• WSDL	
  -­‐	
  Web	
  Services	
  Description	
  Language	
  
• SOAP	
  -­‐	
  Simple	
  Object	
  Access	
  Protocol	
  
	
  
17	
   	
   Error!	
  Reference	
  source	
  not	
  found.	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
	
  
	
  
	
   	
  
	
  
	
  
	
   	
  
 
18	
  
	
  
10.	
   3-­‐Tier	
  Architecure	
  
…	
  
 
19	
  
	
  
11.	
   References	
  
….	
   	
  
	
  
	
  
 
	
  
	
  
	
  
	
  
	
  
Telemark	
  University	
  College	
  
Faculty	
  of	
  Technology	
  
Kjølnes	
  Ring	
  56	
  
N-­‐3914	
  Porsgrunn,	
  Norway	
  
www.hit.no	
  
	
  
	
  
Hans-­‐Petter	
  Halvorsen,	
  M.Sc.	
  
Telemark	
  University	
  College	
  
Faculty	
  of	
  Technology	
  
Department	
  of	
  Electrical	
  Engineering,	
  Information	
  Technology	
  and	
  Cybernetics	
  
	
  
E-­‐mail:	
  hans.p.halvorsen@hit.no	
  
Blog:	
  http://home.hit.no/~hansha/	
  
	
  
	
  
	
  
21	
   	
   Error!	
  Reference	
  source	
  not	
  found.	
   	
  
Tutorial:	
  Using	
  SQL	
  Server	
  in	
  C#	
  -­‐	
  with	
  Examples	
  
	
  

Mais conteúdo relacionado

Mais procurados

Athena java dev guide
Athena java dev guideAthena java dev guide
Athena java dev guide
dvdung
 
SQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10gSQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10g
Leidy Alexandra
 
security-checklist-database
security-checklist-databasesecurity-checklist-database
security-checklist-database
Mohsen B
 

Mais procurados (19)

What is mod_perl?
What is mod_perl?What is mod_perl?
What is mod_perl?
 
6 database
6 database 6 database
6 database
 
Pi Day 2022 - from IoT to MySQL HeatWave Database Service
Pi Day 2022 -  from IoT to MySQL HeatWave Database ServicePi Day 2022 -  from IoT to MySQL HeatWave Database Service
Pi Day 2022 - from IoT to MySQL HeatWave Database Service
 
Excel
ExcelExcel
Excel
 
MFC Whitepaper
MFC WhitepaperMFC Whitepaper
MFC Whitepaper
 
Athena java dev guide
Athena java dev guideAthena java dev guide
Athena java dev guide
 
New Mysql Embedded Guide Wp Nov2012
New Mysql Embedded Guide Wp Nov2012New Mysql Embedded Guide Wp Nov2012
New Mysql Embedded Guide Wp Nov2012
 
Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...
Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...
Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...
 
Share point review qustions
Share point review qustionsShare point review qustions
Share point review qustions
 
SQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10gSQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10g
 
security-checklist-database
security-checklist-databasesecurity-checklist-database
security-checklist-database
 
SQL2SPARQL
SQL2SPARQLSQL2SPARQL
SQL2SPARQL
 
C sharp notes
C sharp notesC sharp notes
C sharp notes
 
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
 
PL/SQL Interview Questions
PL/SQL Interview QuestionsPL/SQL Interview Questions
PL/SQL Interview Questions
 
Esm scg sys_admin_6.0c
Esm scg sys_admin_6.0cEsm scg sys_admin_6.0c
Esm scg sys_admin_6.0c
 
Devoid Web Application From SQL Injection Attack
Devoid Web Application From SQL Injection AttackDevoid Web Application From SQL Injection Attack
Devoid Web Application From SQL Injection Attack
 
Oracle developer interview questions(entry level)
Oracle developer interview questions(entry level)Oracle developer interview questions(entry level)
Oracle developer interview questions(entry level)
 
Concepts of NonStop SQL/MX: Part 4 - Storage.
Concepts of NonStop SQL/MX: Part 4 - Storage.Concepts of NonStop SQL/MX: Part 4 - Storage.
Concepts of NonStop SQL/MX: Part 4 - Storage.
 

Destaque (9)

Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
 
Case Study # Odoo Implementation @ Samuda Chemical Complex Ltd
Case Study # Odoo Implementation @ Samuda Chemical Complex LtdCase Study # Odoo Implementation @ Samuda Chemical Complex Ltd
Case Study # Odoo Implementation @ Samuda Chemical Complex Ltd
 
Efficiencie solutions odoo erp presentation
Efficiencie solutions odoo erp presentationEfficiencie solutions odoo erp presentation
Efficiencie solutions odoo erp presentation
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Download presentation
Download presentationDownload presentation
Download presentation
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Semelhante a Using sql server in c sharp

Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
kaashiv1
 
Sql Injection and Entity Frameworks
Sql Injection and Entity FrameworksSql Injection and Entity Frameworks
Sql Injection and Entity Frameworks
Rich Helton
 
Peoplesoft PIA architecture
Peoplesoft PIA architecturePeoplesoft PIA architecture
Peoplesoft PIA architecture
Amit rai Raaz
 
Microsoft Sql Server 2016 Is Now Live
Microsoft Sql Server 2016 Is Now LiveMicrosoft Sql Server 2016 Is Now Live
Microsoft Sql Server 2016 Is Now Live
Amber Moore
 

Semelhante a Using sql server in c sharp (20)

Php How To
Php How ToPhp How To
Php How To
 
Arc Ready Cloud Computing
Arc Ready Cloud ComputingArc Ready Cloud Computing
Arc Ready Cloud Computing
 
Azure presentation nnug dec 2010
Azure presentation nnug  dec 2010Azure presentation nnug  dec 2010
Azure presentation nnug dec 2010
 
Shrikanth
ShrikanthShrikanth
Shrikanth
 
Oracle sql quick reference
Oracle sql quick referenceOracle sql quick reference
Oracle sql quick reference
 
Web services
Web servicesWeb services
Web services
 
Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
 
Sql interview question part 10
Sql interview question part 10Sql interview question part 10
Sql interview question part 10
 
Project seminar
Project seminarProject seminar
Project seminar
 
ArcReady - Architecting For The Cloud
ArcReady - Architecting For The CloudArcReady - Architecting For The Cloud
ArcReady - Architecting For The Cloud
 
Enabling SQL Access to Data Lakes
Enabling SQL Access to Data LakesEnabling SQL Access to Data Lakes
Enabling SQL Access to Data Lakes
 
SQL Injection - Newsletter
SQL Injection - NewsletterSQL Injection - Newsletter
SQL Injection - Newsletter
 
Sky High With Azure
Sky High With AzureSky High With Azure
Sky High With Azure
 
70487.pdf
70487.pdf70487.pdf
70487.pdf
 
Ado.net
Ado.netAdo.net
Ado.net
 
Sql Injection and Entity Frameworks
Sql Injection and Entity FrameworksSql Injection and Entity Frameworks
Sql Injection and Entity Frameworks
 
application-template-deployment-guide.pdf
application-template-deployment-guide.pdfapplication-template-deployment-guide.pdf
application-template-deployment-guide.pdf
 
Ebook11
Ebook11Ebook11
Ebook11
 
Peoplesoft PIA architecture
Peoplesoft PIA architecturePeoplesoft PIA architecture
Peoplesoft PIA architecture
 
Microsoft Sql Server 2016 Is Now Live
Microsoft Sql Server 2016 Is Now LiveMicrosoft Sql Server 2016 Is Now Live
Microsoft Sql Server 2016 Is Now Live
 

Último

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 

Último (20)

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

Using sql server in c sharp

  • 1.   Telemark  University  College   Department  of  Electrical  Engineering,  Information  Technology  and  Cybernetics   Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01         Using  SQL  Server  in  C#   with  Examples   Hans-­‐Petter  Halvorsen,  2014.03.01      
  • 2.   2       Table  of  Contents   1.   Introduction  .......................................................................................................................  4   2.   SQL  Server  ..........................................................................................................................  5   2.1.   Database  Systems  .......................................................................................................  5   2.2.   Structured  Query  Language  ........................................................................................  5   3.   Visual  Studio  ......................................................................................................................  6   3.1.   The  .NET  Framework  ...................................................................................................  6   3.2.   The  C#  Language  .........................................................................................................  6   4.   Database  Programming  in  C#  .............................................................................................  7   4.1.   ADO.NET  .....................................................................................................................  7   4.2.   Enterprise  Library  &  Data  Access  Application  Block  ...................................................  8   4.3.   LINQ  ............................................................................................................................  8   4.3.1.   LINQ  to  SQL  ..........................................................................................................  8   4.4.   Entity  Framework  ........................................................................................................  8   4.4.1.   LINQ  to  Entities  ....................................................................................................  8   5.   ADO.NET  ..........................................................................................................................  10   5.1.   Introduction  ..............................................................................................................  10   5.2.   Examples  ...................................................................................................................  10   6.   Enterprise  Library  .............................................................................................................  11   6.1.   Data  Access  Application  Block  ..................................................................................  11   6.1.1.   Installation  .........................................................................................................  11   7.   LINQ  .................................................................................................................................  13  
  • 3. 3     Table  of  Contents     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples   7.1.   LINQ  to  SQL  ...............................................................................................................  13   8.   Entity  Framework  .............................................................................................................  14   8.1.   …  ...............................................................................................................................  14   9.   Web  Services  ....................................................................................................................  15   10.   3-­‐Tier  Architecure  ..........................................................................................................  18   11.   References  .....................................................................................................................  19    
  • 4.   4     1.   Introduction   This  Tutorial  gives  an  overview  of  different  techniques  for  communicationg  with  SQL  Server   in  Visual  Studio  and  C#.   For  more  information  about  Software  and  Programming,  please  see  my  web  site  located   here:   http://home.hit.no/~hansha/            
  • 5.   5     2.   SQL  Server   …   2.1.   Database  Systems   …   For  more  information  about  Database  Systems,  please  see  the  Tutorial  “Introduction  to   Database  Systems”  located  here:   http://home.hit.no/~hansha/?tutorial=database     2.2.   Structured  Query  Language   …   For  more  information  about  SQL,  please  see  the  Tutorial  “Structured  Query  Language”   located  here:   http://home.hit.no/~hansha/?tutorial=sql          
  • 6.   6     3.   Visual  Studio   Microsoft  Visual  Studio  is  an  integrated  development  environment  (IDE)  from  Microsoft.   Home  page  of  Visual  Studio:  http://www.visualstudio.com     For  more  information  about  Visual  Studio  and  C#,  please  see  the  Tutorial  “Introduction  to   Visual  Studio  and  C#”  located  here:   http://home.hit.no/~hansha/?tutorial=csharp   3.1.   The  .NET  Framework   The  .NET  Framework  (pronounced  “dot  net”)  is  a  software  framework  that  runs  primarily  on   Microsoft  Windows.  It  includes  a  large  library  and  supports  several  programming  languages   such  as  C#.  .NET  is  included  with  Visual  Studio.   3.2.   The  C#  Language   C#  is  pronounced  “see  sharp”.  C#  is  an  object-­‐oriented  programming  language  and  part  of   the  .NET  family  from  Microsoft.  C#  is  very  similar  to  C++  and  Java.  C#  is  developed  by   Microsoft  and  works  only  on  the  Windows  platform.              
  • 7.   7     4.   Database  Programming   in  C#   Visual  Studio,  .NET  and  C#  offers  lots  of  different  techniques  for  communicationg  with   Databases,  espessially  SQL  Server.   Some  of  the  techniques  discussed  in  this  document  are:   • ADO.NET   • Enterprise  Library  &  Data  Access  Application  Block   • LINQ   o LINQ  to  SQL   o LINQ  to  Enitities   • Entity  Framework   4.1.   ADO.NET   ADO.NET  is  the  basic  data-­‐access  technology  for  .NET.   ADO.NET  (ActiveX  Data  Object  for  .NET)  is  a  set  of  computer  software  components  that   programmers  can  use  to  access  data  and  data  services.  It  is  a  part  of  the  base  class  library   that  is  included  with  the  Microsoft  .NET  Framework.  It  is  commonly  used  by  programmers  to   access  and  modify  data  stored  in  relational  database  systems,  though  it  can  also  access  data   in  non-­‐relational  sources.   http://msdn.microsoft.com/en-­‐us/library/e80y5yhx(v=vs.110).aspx   ADO.NET  provides  consistent  access  to  data  sources  such  as  SQL  Server  and  XML,  and  to   data  sources  exposed  through  OLE  DB  and  ODBC.   The  ADO.NET  classes  are  found  in  System.Data.dll.     ADO.NET  Code  Examples:   http://msdn.microsoft.com/en-­‐us/library/dw70f090(v=vs.110).aspx    
  • 8. 8     Error!  Reference  source  not  found.     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples   4.2.   Enterprise  Library  &  Data  Access   Application  Block   Microsoft  Enterprise  Library  is  a  collection  of  different  libraries  that  makes  your  life  as  a   programmer  easier.  In  this  document  we  will  only  use  the  Data  Access  Application  Block.   The  Data  Access  Application  Block  simplifies  many  common  data  access  tasks  such  as   reading  data  for  display,  passing  data  through  application  layers,  and  submitting  changed   data  back  to  the  database  system.  It  includes  support  for  both  stored  procedures  and  in-­‐line   SQL,  can  expose  the  data  as  a  sequence  of  objects  for  client-­‐side  querying,  and  provides   access  to  the  most  frequently  used  features  of  ADO.NET  in  simple-­‐to-­‐use  classes.   You  can  use  the  NuGet  Package  Manager  in  Visual  Studio  to  install  the  Enterprise  Library   assemblies  that  you  need  in  your  projects.   Data  Access  Application  Block  Web  Site:   http://msdn.microsoft.com/en-­‐us/library/dn440726(v=pandp.60).aspx     4.3.   LINQ   …   4.3.1.   LINQ  to  SQL   LINQ  to  SQL  is  a  method  to  work  with  Microsoft  SQL  Server  databases  using  LINQ.     Microsoft  recommends  that  you  use  Entity  Framework  for  new  applications,  because  LINQ   to  SQL  will  not  be  further  developed.   4.4.   Entity  Framework   …   Entity  Framework  Overview:   http://msdn.microsoft.com/en-­‐us/library/bb399567(v=vs.110).aspx     4.4.1.   LINQ  to  Entities  
  • 9. 9     Error!  Reference  source  not  found.     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples   …      
  • 10.   10     5.   ADO.NET   …   5.1.   Introduction   …   5.2.   Examples   …    
  • 11.   11     6.   Enterprise  Library   Enterprise  Library  is  part  of  Microsoft  Patters  &  Practices.  Microsoft  Patters  &  Practices  are   recommendations  on  how  to  design  and  develop  custom  applications  using  the  Microsoft   platform.     Microsoft  Patters  &  Practices  Web  Site:   http://msdn.microsoft.com/en-­‐us/library/ff921345.aspx     Enterprise  Library  Web  Site:  http://msdn.microsoft.com/en-­‐us/library/ff648951.aspx     Microsoft  Enterprise  Library  is  a  collection  of  different  libraries  that  makes  your  life  as  a   programmer  easier.  In  this  document  we  will  only  use  the  Data  Access  Application  Block.   6.1.   Data  Access  Application  Block   The  Data  Access  Application  Block  simplifies  many  common  data  access  tasks  such  as   reading  data  for  display,  passing  data  through  application  layers,  and  submitting  changed   data  back  to  the  database  system.  It  includes  support  for  both  stored  procedures  and  in-­‐line   SQL,  can  expose  the  data  as  a  sequence  of  objects  for  client-­‐side  querying,  and  provides   access  to  the  most  frequently  used  features  of  ADO.NET  in  simple-­‐to-­‐use  classes.   6.1.1.   Installation   You  can  use  the  NuGet  Package  Manager  in  Visual  Studio  to  install  the  Enterprise  Library   assemblies  that  you  need  in  your  projects.   Installation  of  the  The  Data  Access  Application  Block:  
  • 12. 12     Error!  Reference  source  not  found.     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples        
  • 13.   13     7.   LINQ   …   7.1.   LINQ  to  SQL   …  
  • 14.   14     8.   Entity  Framework   …   8.1.   …   …          
  • 15.   15     9.   Web  Services   In  many  situations  our  apps  don’t  necessarly  have  direct  access  to  the  datbase,  especially   when  your  sofware  is  used  ourside  the  local  company  network.  In  these  situations  we  need   to  use  a  Web  Service  in  order  to  write  or  read  data  from  the  database.     Today  Web  Services  have  been  very  popular.  A  Web  service  is  a  method  of  communications   between  two  devices  over  the  World  Wide  Web,  and  makes  it  easy  to  share  data  over  a   network  or  the  internet.   A  Web  Service  is   • A  Web  API   • A  Standard  defined  by  W3C   • Cross-­‐platform  and  Platform-­‐independent  Communication   • Distributed  Application  Development     Web  Services  can  be  implemented  and  used  in  most  Programming  Languages  (C#/ASP.NET,   PHP,  LabVIEW,  Objective-­‐C,  Java,  etc.)  
  • 16. 16     Error!  Reference  source  not  found.     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples   Web  Services  uses  standard  Web  technology  (Web  protocols)  such  as  HTTP,  REST,  SOAP,   XML,  WSDL,  JSON,  etc.     Web  Services  technology  used  in  Web  Services:   • HTTP  -­‐  Hypertext  Transfer  Protocol   • XML  –  Extensible  Markup  Language   • WSDL  -­‐  Web  Services  Description  Language   • SOAP  -­‐  Simple  Object  Access  Protocol    
  • 17. 17     Error!  Reference  source  not  found.     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples                  
  • 18.   18     10.   3-­‐Tier  Architecure   …  
  • 19.   19     11.   References   ….        
  • 20.             Telemark  University  College   Faculty  of  Technology   Kjølnes  Ring  56   N-­‐3914  Porsgrunn,  Norway   www.hit.no       Hans-­‐Petter  Halvorsen,  M.Sc.   Telemark  University  College   Faculty  of  Technology   Department  of  Electrical  Engineering,  Information  Technology  and  Cybernetics     E-­‐mail:  hans.p.halvorsen@hit.no   Blog:  http://home.hit.no/~hansha/        
  • 21. 21     Error!  Reference  source  not  found.     Tutorial:  Using  SQL  Server  in  C#  -­‐  with  Examples