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 (17)

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

Viewers also liked (20)

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

Similar to The sql connection object (20)

PPT
For Beginers - ADO.Net
Snehal Harawande
 
PPT
Synapseindia dot net development chapter 8 asp dot net
Synapseindiappsdevelopment
 
PPTX
Chapter 14
application developer
 
PDF
Ado.Net Architecture
Umar Farooq
 
PPTX
Asp .Net Database Connectivity Presentation.pptx
sridharu1981
 
PPT
ASP.NET 09 - ADO.NET
Randy Connolly
 
PPT
ASP.NET Session 11 12
Sisir Ghosh
 
PPTX
Ado.net
pacatarpit
 
PPT
ADO.NET
Wani Zahoor
 
PPT
Marmagna desai
jmsthakur
 
PDF
Sql a practical introduction
Hasan Kata
 
PDF
Sql a practical introduction
sanjaychauhan689
 
DOCX
Simple ado program by visual studio
Aravindharamanan S
 
DOCX
Simple ado program by visual studio
Aravindharamanan S
 
PPTX
Chapter 3: ado.net
Ngeam Soly
 
PPT
the .NET Framework. It provides the claf
TesfahunMaru1
 
PPT
Creating Database 2010
tgushi12
 
PPTX
Csharp_dotnet_ADO_Net_database_query.pptx
facebookrecovery1
 
PPT
Ado.net
Iblesoft
 
PPTX
Web based database application design using vb.net and sql server
Ammara Arooj
 
For Beginers - ADO.Net
Snehal Harawande
 
Synapseindia dot net development chapter 8 asp dot net
Synapseindiappsdevelopment
 
Ado.Net Architecture
Umar Farooq
 
Asp .Net Database Connectivity Presentation.pptx
sridharu1981
 
ASP.NET 09 - ADO.NET
Randy Connolly
 
ASP.NET Session 11 12
Sisir Ghosh
 
Ado.net
pacatarpit
 
ADO.NET
Wani Zahoor
 
Marmagna desai
jmsthakur
 
Sql a practical introduction
Hasan Kata
 
Sql a practical introduction
sanjaychauhan689
 
Simple ado program by visual studio
Aravindharamanan S
 
Simple ado program by visual studio
Aravindharamanan S
 
Chapter 3: ado.net
Ngeam Soly
 
the .NET Framework. It provides the claf
TesfahunMaru1
 
Creating Database 2010
tgushi12
 
Csharp_dotnet_ADO_Net_database_query.pptx
facebookrecovery1
 
Ado.net
Iblesoft
 
Web based database application design using vb.net and sql server
Ammara Arooj
 

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();