SlideShare a Scribd company logo
The SqlConnection Object
• The first thing you will need to do when
  interacting with a data base is to create a
  connection.
Creating a SqlConnection Object

• A SqlConnection is an object,
• SqlConnection conn = new SqlConnection(
  "Data Source=(local);Initial
  Catalog=Northwind;Integrated
  Security=SSPI");
• SqlConnection conn = new SqlConnection(
  "Data Source=DatabaseServer;Initial
  Catalog=Northwind;User
  ID=YourUserID;Password=YourPassword");
Using a SqlConnection

• Instantiate the SqlConnection.
• Open the connection.
• Pass the connection to other ADO.NET
  objects.
• Perform database operations with the other
  ADO.NET objects.
• Close the connection.
The SqlCommand Object

• A SqlCommand object allows you to specify
  what type of interaction you want to perform
  with a database.
• SqlCommand cmd = new SqlCommand("select
  CategoryName from Categories", conn);
• // 1. Instantiate a new command with a query
  and connection
  SqlCommand cmd = new SqlCommand("select
  CategoryName from Categories", conn);

 // 2. Call Execute reader to get query results
 SqlDataReader rdr = cmd.ExecuteReader();
Inserting Data

• To insert data into a database, use the
  ExecuteNonQuery method of the
  SqlCommand object.
// prepare command string
• string insertString = @"
      insert into Categories
      (CategoryName, Description)
      values ('Miscellaneous', 'Whatever doesn''t
  fit elsewhere')"
• // 1. Instantiate a new command with a query
  and connection
   SqlCommand cmd
  = new SqlCommand(insertString, conn);

  // 2. Call ExecuteNonQuery to send command
  cmd.ExecuteNonQuery();
Updating Data

• // prepare command string
   string updateString = @"
      update Categories
      set CategoryName = 'Other'
      where CategoryName = 'Miscellaneous'";
• // 1. Instantiate a new command with
  command text only
   SqlCommand cmd
  = new SqlCommand(updateString);

  // 2. Set the Connection property
  cmd.Connection = conn;

  // 3. Call ExecuteNonQuery to send command
  cmd.ExecuteNonQuery();
Deleting Data

• // prepare command string
   string deleteString = @"
      delete from Categories
      where CategoryName = 'Other'";
• // 1. Instantiate a new command
   SqlCommand cmd = new SqlCommand();
  // 2. Set the CommandText property
  cmd.CommandText = deleteString;
  // 3. Set the Connection property
  cmd.Connection = conn;
  // 4. Call ExecuteNonQuery to send command
  cmd.ExecuteNonQuery();
Getting Single values

• Sometimes all you need from a database is a
  single value, which could be a
  count, sum, average, or other aggregated
  value from a data set.
• // 1. Instantiate a new command
   SqlCommand cmd
  = new SqlCommand("select count(*) from
  Categories", conn);

  // 2. Call ExecuteNonQuery to send command
  int count = (int)cmd.ExecuteScalar();

More Related Content

What's hot

Dagger1
Dagger1Dagger1
Dagger1
Ramesh Akula
 
Lesson3
Lesson3Lesson3
Presentation cs313 (1)
Presentation cs313 (1)Presentation cs313 (1)
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wild
Brainhub
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
DIY Percolator
DIY PercolatorDIY Percolator
DIY Percolator
jdhok
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, Macoscope
Macoscope
 
บทที่3
บทที่3บทที่3
บทที่3
Palm Unnop
 
บทที่4
บทที่4บทที่4
บทที่4
Waritsara Sonchan
 
09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP
Nguyen Tuan
 
4 gouping object
4 gouping object4 gouping object
4 gouping object
Robbie AkaChopa
 
Oop presentation
Oop presentationOop presentation
Oop presentation
Ch shampi Ch shampi
 
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูลบทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
Priew Chakrit
 
(E Book) Asp .Net Tips, Tutorials And Code
(E Book) Asp .Net Tips,  Tutorials And Code(E Book) Asp .Net Tips,  Tutorials And Code
(E Book) Asp .Net Tips, Tutorials And Code
syedjee
 
Android Architecure Components - introduction
Android Architecure Components - introductionAndroid Architecure Components - introduction
Android Architecure Components - introduction
Paulina Szklarska
 
Cassandra Day Atlanta 2015: Data Modeling 101
Cassandra Day Atlanta 2015: Data Modeling 101Cassandra Day Atlanta 2015: Data Modeling 101
Cassandra Day Atlanta 2015: Data Modeling 101
DataStax Academy
 

What's hot (17)

Dagger1
Dagger1Dagger1
Dagger1
 
Lesson3
Lesson3Lesson3
Lesson3
 
Presentation cs313 (1)
Presentation cs313 (1)Presentation cs313 (1)
Presentation cs313 (1)
 
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wild
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
 
DIY Percolator
DIY PercolatorDIY Percolator
DIY Percolator
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, Macoscope
 
บทที่3
บทที่3บทที่3
บทที่3
 
บทที่4
บทที่4บทที่4
บทที่4
 
09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP
 
4 gouping object
4 gouping object4 gouping object
4 gouping object
 
Oop presentation
Oop presentationOop presentation
Oop presentation
 
Insert
InsertInsert
Insert
 
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูลบทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
บทที่ 4 การเพิ่มข้อมูลลงฐานข้อมูล
 
(E Book) Asp .Net Tips, Tutorials And Code
(E Book) Asp .Net Tips,  Tutorials And Code(E Book) Asp .Net Tips,  Tutorials And Code
(E Book) Asp .Net Tips, Tutorials And Code
 
Android Architecure Components - introduction
Android Architecure Components - introductionAndroid Architecure Components - introduction
Android Architecure Components - introduction
 
Cassandra Day Atlanta 2015: Data Modeling 101
Cassandra Day Atlanta 2015: Data Modeling 101Cassandra Day Atlanta 2015: Data Modeling 101
Cassandra Day Atlanta 2015: Data Modeling 101
 

Viewers also liked

Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Alphorm.com Formation Java Server Faces
Alphorm.com Formation Java Server FacesAlphorm.com Formation Java Server Faces
Alphorm.com Formation Java Server Faces
Alphorm
 
Chatbots in HR: Improving the Employee Experience
Chatbots in HR: Improving the Employee ExperienceChatbots in HR: Improving the Employee Experience
Chatbots in HR: Improving the Employee Experience
Amy Kong
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programming
Exotel
 
CSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, LinzCSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, Linz
Rachel Andrew
 
Brand New World
Brand New WorldBrand New World
Brand New World
Torsten Henning Hensel
 
How to Pitch B2B
How to Pitch B2BHow to Pitch B2B
How to Pitch B2B
Slides That Rock
 
26 Top Crowdfunding Sites (Infographic)
26 Top Crowdfunding Sites (Infographic)26 Top Crowdfunding Sites (Infographic)
26 Top Crowdfunding Sites (Infographic)
Wrike
 
How To Assemble a High Converting eBook
How To Assemble a High Converting eBookHow To Assemble a High Converting eBook
How To Assemble a High Converting eBook
Uberflip
 
Social Proof Tips to Boost Landing Page Conversions
Social Proof Tips to Boost Landing Page ConversionsSocial Proof Tips to Boost Landing Page Conversions
Social Proof Tips to Boost Landing Page Conversions
Angie Schottmuller
 
100 growth hacks 100 days | 1 to 10
100 growth hacks 100 days | 1 to 10100 growth hacks 100 days | 1 to 10
100 growth hacks 100 days | 1 to 10
Robin Yjord
 
10 Disruptive Quotes for Entrepreneurs
10 Disruptive Quotes for Entrepreneurs10 Disruptive Quotes for Entrepreneurs
10 Disruptive Quotes for Entrepreneurs
Guy Kawasaki
 
Pitching Ideas: How to sell your ideas to others
Pitching Ideas: How to sell your ideas to othersPitching Ideas: How to sell your ideas to others
Pitching Ideas: How to sell your ideas to others
Jeroen van Geel
 
17 Copywriting Do's and Don'ts: How To Write Persuasive Content
17 Copywriting Do's and Don'ts: How To Write Persuasive Content17 Copywriting Do's and Don'ts: How To Write Persuasive Content
17 Copywriting Do's and Don'ts: How To Write Persuasive Content
Henneke Duistermaat
 
Create icons in PowerPoint
Create icons in PowerPointCreate icons in PowerPoint
Create icons in PowerPoint
Presentitude
 
The Ultimate Freebies Guide for Presentations by @damonify
The Ultimate Freebies Guide for Presentations by @damonifyThe Ultimate Freebies Guide for Presentations by @damonify
The Ultimate Freebies Guide for Presentations by @damonify
Slides | Presentation Design Agency
 
Europas BlaBlaCar pitch
Europas BlaBlaCar pitchEuropas BlaBlaCar pitch
Europas BlaBlaCar pitch
Vanina Schick
 
Email Marketing 101: The Welcome Email
Email Marketing 101: The Welcome EmailEmail Marketing 101: The Welcome Email
Email Marketing 101: The Welcome Email
SendGrid
 
Quick & Dirty Tips for : Better PowerPoint Presentations Faster
Quick & Dirty Tips for : Better PowerPoint Presentations FasterQuick & Dirty Tips for : Better PowerPoint Presentations Faster
Quick & Dirty Tips for : Better PowerPoint Presentations Faster
Eugene Cheng
 
The Sharing Economy
The Sharing EconomyThe Sharing Economy
The Sharing Economy
Loic Le Meur
 

Viewers also liked (20)

Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Alphorm.com Formation Java Server Faces
Alphorm.com Formation Java Server FacesAlphorm.com Formation Java Server Faces
Alphorm.com Formation Java Server Faces
 
Chatbots in HR: Improving the Employee Experience
Chatbots in HR: Improving the Employee ExperienceChatbots in HR: Improving the Employee Experience
Chatbots in HR: Improving the Employee Experience
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programming
 
CSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, LinzCSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, Linz
 
Brand New World
Brand New WorldBrand New World
Brand New World
 
How to Pitch B2B
How to Pitch B2BHow to Pitch B2B
How to Pitch B2B
 
26 Top Crowdfunding Sites (Infographic)
26 Top Crowdfunding Sites (Infographic)26 Top Crowdfunding Sites (Infographic)
26 Top Crowdfunding Sites (Infographic)
 
How To Assemble a High Converting eBook
How To Assemble a High Converting eBookHow To Assemble a High Converting eBook
How To Assemble a High Converting eBook
 
Social Proof Tips to Boost Landing Page Conversions
Social Proof Tips to Boost Landing Page ConversionsSocial Proof Tips to Boost Landing Page Conversions
Social Proof Tips to Boost Landing Page Conversions
 
100 growth hacks 100 days | 1 to 10
100 growth hacks 100 days | 1 to 10100 growth hacks 100 days | 1 to 10
100 growth hacks 100 days | 1 to 10
 
10 Disruptive Quotes for Entrepreneurs
10 Disruptive Quotes for Entrepreneurs10 Disruptive Quotes for Entrepreneurs
10 Disruptive Quotes for Entrepreneurs
 
Pitching Ideas: How to sell your ideas to others
Pitching Ideas: How to sell your ideas to othersPitching Ideas: How to sell your ideas to others
Pitching Ideas: How to sell your ideas to others
 
17 Copywriting Do's and Don'ts: How To Write Persuasive Content
17 Copywriting Do's and Don'ts: How To Write Persuasive Content17 Copywriting Do's and Don'ts: How To Write Persuasive Content
17 Copywriting Do's and Don'ts: How To Write Persuasive Content
 
Create icons in PowerPoint
Create icons in PowerPointCreate icons in PowerPoint
Create icons in PowerPoint
 
The Ultimate Freebies Guide for Presentations by @damonify
The Ultimate Freebies Guide for Presentations by @damonifyThe Ultimate Freebies Guide for Presentations by @damonify
The Ultimate Freebies Guide for Presentations by @damonify
 
Europas BlaBlaCar pitch
Europas BlaBlaCar pitchEuropas BlaBlaCar pitch
Europas BlaBlaCar pitch
 
Email Marketing 101: The Welcome Email
Email Marketing 101: The Welcome EmailEmail Marketing 101: The Welcome Email
Email Marketing 101: The Welcome Email
 
Quick & Dirty Tips for : Better PowerPoint Presentations Faster
Quick & Dirty Tips for : Better PowerPoint Presentations FasterQuick & Dirty Tips for : Better PowerPoint Presentations Faster
Quick & Dirty Tips for : Better PowerPoint Presentations Faster
 
The Sharing Economy
The Sharing EconomyThe Sharing Economy
The Sharing Economy
 

Similar to The sql connection object

For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
Snehal Harawande
 
ADO.NETObjects
ADO.NETObjectsADO.NETObjects
ADO.NETObjects
Wings Interactive
 
Sql server-function
Sql server-functionSql server-function
Sql server-function
AnkushAgrawal56
 
3-ADO.NET.pdf
3-ADO.NET.pdf3-ADO.NET.pdf
3-ADO.NET.pdf
ManalAg
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptx
sridharu1981
 
Parameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputParameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple input
uanna
 
Fetch data from form
Fetch data from formFetch data from form
Fetch data from form
Shahriar Malik
 
ADO.NET
ADO.NETADO.NET
ADO.NET
Wani Zahoor
 
Database security
Database securityDatabase security
Database security
Rambabu Duddukuri
 
ADO.net control
ADO.net controlADO.net control
ADO.net control
Paneliya Prince
 
SQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdfSQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdf
Mona686896
 
Ado.net
Ado.netAdo.net
Ado.net
Iblesoft
 
37c
37c37c
Whitepaper To Study Filestream Option In Sql Server
Whitepaper To Study Filestream Option In Sql ServerWhitepaper To Study Filestream Option In Sql Server
Whitepaper To Study Filestream Option In Sql Server
Shahzad
 
Linq
LinqLinq
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
info_zybotech
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
info_zybotech
 
Database c# connetion
Database c# connetionDatabase c# connetion
Database c# connetion
Christofer Toledo
 
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISEWINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
Hitesh Mohapatra
 

Similar to The sql connection object (20)

For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
ADO.NETObjects
ADO.NETObjectsADO.NETObjects
ADO.NETObjects
 
Sql server-function
Sql server-functionSql server-function
Sql server-function
 
3-ADO.NET.pdf
3-ADO.NET.pdf3-ADO.NET.pdf
3-ADO.NET.pdf
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptx
 
Parameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputParameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple input
 
Fetch data from form
Fetch data from formFetch data from form
Fetch data from form
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Database security
Database securityDatabase security
Database security
 
ADO.net control
ADO.net controlADO.net control
ADO.net control
 
SQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdfSQL Server with CSharp WinForms.pdf
SQL Server with CSharp WinForms.pdf
 
Ado.net
Ado.netAdo.net
Ado.net
 
37c
37c37c
37c
 
Whitepaper To Study Filestream Option In Sql Server
Whitepaper To Study Filestream Option In Sql ServerWhitepaper To Study Filestream Option In Sql Server
Whitepaper To Study Filestream Option In Sql Server
 
Linq
LinqLinq
Linq
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
Database c# connetion
Database c# connetionDatabase c# connetion
Database c# connetion
 
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISEWINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
 

The sql connection object

  • 2. • The first thing you will need to do when interacting with a data base is to create a connection.
  • 3. Creating a SqlConnection Object • A SqlConnection is an object, • SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"); • SqlConnection conn = new SqlConnection( "Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword");
  • 4. Using a SqlConnection • Instantiate the SqlConnection. • Open the connection. • Pass the connection to other ADO.NET objects. • Perform database operations with the other ADO.NET objects. • Close the connection.
  • 5. The SqlCommand Object • A SqlCommand object allows you to specify what type of interaction you want to perform with a database. • SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
  • 6. • // 1. Instantiate a new command with a query and connection SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn); // 2. Call Execute reader to get query results SqlDataReader rdr = cmd.ExecuteReader();
  • 7. Inserting Data • To insert data into a database, use the ExecuteNonQuery method of the SqlCommand object.
  • 8. // prepare command string • string insertString = @" insert into Categories (CategoryName, Description) values ('Miscellaneous', 'Whatever doesn''t fit elsewhere')"
  • 9. • // 1. Instantiate a new command with a query and connection SqlCommand cmd = new SqlCommand(insertString, conn); // 2. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery();
  • 10. Updating Data • // prepare command string string updateString = @" update Categories set CategoryName = 'Other' where CategoryName = 'Miscellaneous'";
  • 11. • // 1. Instantiate a new command with command text only SqlCommand cmd = new SqlCommand(updateString); // 2. Set the Connection property cmd.Connection = conn; // 3. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery();
  • 12. Deleting Data • // prepare command string string deleteString = @" delete from Categories where CategoryName = 'Other'";
  • 13. • // 1. Instantiate a new command SqlCommand cmd = new SqlCommand(); // 2. Set the CommandText property cmd.CommandText = deleteString; // 3. Set the Connection property cmd.Connection = conn; // 4. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery();
  • 14. Getting Single values • Sometimes all you need from a database is a single value, which could be a count, sum, average, or other aggregated value from a data set.
  • 15. • // 1. Instantiate a new command SqlCommand cmd = new SqlCommand("select count(*) from Categories", conn); // 2. Call ExecuteNonQuery to send command int count = (int)cmd.ExecuteScalar();