SlideShare a Scribd company logo
DATABASE
CONNECTIVITY
Programming : C#
Technology : Asp.net
Database : MS Sql Server
Author : Hemant Sankhla (Web Developer)
I will teach you how to connect MS Sql Server
Database to an Asp.Net Web Application using C#.
After the successful connection you are able to
perform various operations on database from your
web application like select, insert, update, delete etc.
Lets check it out and do yourself.
Author : Hemant Sankhla (Web Developer)
First of all Create Database in MS Sql Server
1. Go to Database > Right Click > New Database. And give a
name to your Database.
2. Now Create a table in this database. To do such select the
database > Right click On Table > New Table. Now enter
desired fields and save it using ctrl + s.
Now your database with tables is ready to be a part of your
web application.
Author : Hemant Sankhla (Web Developer)
Create The Connection String (Path to server and database)
Connection string means the address to the Sql Server and The database using the
security access, or if there is an username and password then providing these
credentials for accessing the server and database.
Now type below line in ‘web.config’ file which you have in your website directory,
search for it in Solution Explorer. If we make connection in ‘web.config’ file then we
only have to make it once and we call it where we want it.
<connectionStrings>
<add name = "con" connectionString="Data Source=.sqlexpress ;
Initial Catalog=Database; Integrated Security=true"/>
</connectionStrings>
Author : Hemant Sankhla (Web Developer)
Create The Connection String (Path to server and database)
name : The name of your connection
ConnectionString : The full path or address to your server and database
DataSource : Sql Server Name (you can get this in Sql server management studio.
Copy the server name from the connection dialog box at starting.)
Initial Catalog : Your Database.
Integrated Security : True, use this if you have no username and password in sql
server.
Author : Hemant Sankhla (Web Developer)
Using NameSpaces
Namespace is a collection of classes and methods. In C# various
namespaces are used in different work. Here we want to connect and use
the Sql Server so we have to use appropriate namespaces for this task.
Namespaces are used by the ‘Using keyword.’
Add these two name spaces.
using System.Data;
using System.Data.SqlClient;
Author : Hemant Sankhla (Web Developer)
Using NameSpaces
Namespace is a collection of classes and methods. In C# various
namespaces are used in different work. Here we want to connect and use
the Sql Server so we have to use appropriate namespaces for this task.
Namespaces are used by the ‘Using keyword.’
Add these two name spaces.
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
Author : Hemant Sankhla (Web Developer)
The Essential Code
Below is the essential code for all type of operation.
SqlConnection con = new SqlConnection
(WebConfigurationManager.ConnectionStrings["con"].ToString( ));
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
Author : Hemant Sankhla (Web Developer)
Understanding The Code
SqlConnection : This class creates a connection object. Con is the object
for connection.
WebConfigurationManager : This class fetch the connection string from
the web.config file.
Con.open( ) : Open the current connection.
SqlCommand : Create the Sql command Query’s object.
Cmd.connection = con : Supply the con object to the cmd connection.
Author : Hemant Sankhla (Web Developer)
Select All
SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToStrin
g());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from Table ";
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
Author : Hemant Sankhla (Web Developer)
Insert Into
SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into Student (FullName, Marks) values ('" +
txtName.Text + "','" + txtMarks.Text + "')";
int h = cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write("Inserted Successfully");
}
Author : Hemant Sankhla (Web Developer)
Update Record
SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = “update Student SET FullName = '" + txtName.Text + "',
Marks = '" + txtMarks.Text + “’ where ID = ID;
int h = cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write(“Record Updated Successfully");
}
Author : Hemant Sankhla (Web Developer)
Delete Record
SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = “delete from Student where ID =‘ ” + ID + “ ‘;
int h = cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write(“Record Deleted Successfully");
}
Author : Hemant Sankhla (Web Developer)
Now you have to practice those codes by hand. If you feel any trouble comment me
These codes also work well in C# Windows Desktop Application.
Author : Hemant Sankhla (Web Developer)

More Related Content

What's hot (20)

PPTX
Multithreading in java
Raghu nath
 
DOC
Dbms lab Manual
Vivek Kumar Sinha
 
PPTX
Presentation on array
topu93
 
PPT
Oracle PLSQL Step By Step Guide
Srinimf-Slides
 
PPTX
sql function(ppt)
Ankit Dubey
 
PPT
Constraints In Sql
Anurag
 
PPT
C# Exceptions Handling
sharqiyem
 
PPT
Mysql
TSUBHASHRI
 
PPTX
SQL - DML and DDL Commands
Shrija Madhu
 
PPTX
Sqlmap
shamshad9
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
SQL commands
GirdharRatne
 
PPTX
Java string handling
Salman Khan
 
PPTX
Adbms 16 object definition language
Vaibhav Khanna
 
PPT
Introduction to structured query language (sql)
Sabana Maharjan
 
DOC
DBMS Practical File
Dushmanta Nath
 
PPTX
Templates in c++
ThamizhselviKrishnam
 
PPTX
Union in C programming
Kamal Acharya
 
PPTX
Overloading and overriding in vb.net
suraj pandey
 
Multithreading in java
Raghu nath
 
Dbms lab Manual
Vivek Kumar Sinha
 
Presentation on array
topu93
 
Oracle PLSQL Step By Step Guide
Srinimf-Slides
 
sql function(ppt)
Ankit Dubey
 
Constraints In Sql
Anurag
 
C# Exceptions Handling
sharqiyem
 
Mysql
TSUBHASHRI
 
SQL - DML and DDL Commands
Shrija Madhu
 
Sqlmap
shamshad9
 
SQL commands
GirdharRatne
 
Java string handling
Salman Khan
 
Adbms 16 object definition language
Vaibhav Khanna
 
Introduction to structured query language (sql)
Sabana Maharjan
 
DBMS Practical File
Dushmanta Nath
 
Templates in c++
ThamizhselviKrishnam
 
Union in C programming
Kamal Acharya
 
Overloading and overriding in vb.net
suraj pandey
 

Viewers also liked (20)

PPTX
13206532 pss7
Rubyming
 
PPTX
Bluetooth - Replacement Of Wires
Hemant Sankhla
 
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
PDF
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
 
PDF
Web I - 05 - HTTP Protocol
Randy Connolly
 
PDF
Modern c++ (C++ 11/14)
Geeks Anonymes
 
PDF
Bjarne essencegn13
Hunde Gurmessa
 
PPTX
C++11
Quang Trần Duy
 
PDF
C++11
ppd1961
 
PDF
C++11 concurrency
xu liwei
 
PPTX
Asp Net Advance Topics
Ali Taki
 
PDF
C++11 & C++14
CyberPlusIndia
 
PDF
"Http protocol and other stuff" by Bipin Upadhyay
Bipin Upadhyay
 
PPTX
HTTP Protocol Basic
Chuong Mai
 
PDF
C++17 - the upcoming revolution (Code::Dive 2015)/
Sławomir Zborowski
 
PPTX
Elements of C++11
Uilian Ries
 
PDF
Networking - TCP/IP stack introduction and IPv6
Rodolfo Kohn
 
PDF
C++11 Idioms @ Silicon Valley Code Camp 2012
Sumant Tambe
 
PPTX
ASP.NET State management
Shivanand Arur
 
13206532 pss7
Rubyming
 
Bluetooth - Replacement Of Wires
Hemant Sankhla
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
 
Web I - 05 - HTTP Protocol
Randy Connolly
 
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Bjarne essencegn13
Hunde Gurmessa
 
C++11
ppd1961
 
C++11 concurrency
xu liwei
 
Asp Net Advance Topics
Ali Taki
 
C++11 & C++14
CyberPlusIndia
 
"Http protocol and other stuff" by Bipin Upadhyay
Bipin Upadhyay
 
HTTP Protocol Basic
Chuong Mai
 
C++17 - the upcoming revolution (Code::Dive 2015)/
Sławomir Zborowski
 
Elements of C++11
Uilian Ries
 
Networking - TCP/IP stack introduction and IPv6
Rodolfo Kohn
 
C++11 Idioms @ Silicon Valley Code Camp 2012
Sumant Tambe
 
ASP.NET State management
Shivanand Arur
 
Ad

Similar to Database connectivity to sql server asp.net (20)

PPTX
Chapter 14
application developer
 
PPTX
Asp .Net Database Connectivity Presentation.pptx
sridharu1981
 
PPT
Synapseindia dot net development chapter 8 asp dot net
Synapseindiappsdevelopment
 
ODP
Ado Presentation
Rebecca Peltz
 
DOCX
unit 3.docx
Sadhana Sreekanth
 
PPTX
The sql connection object
Sharat Chandu
 
PDF
SQL Server with CSharp WinForms.pdf
Mona686896
 
PPT
For Beginers - ADO.Net
Snehal Harawande
 
PPTX
Database connectivity in asp.net
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Using sql server in c sharp
Faruk Alkan
 
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
samimylahji
 
PDF
04. SQL Servesdafr with CSharp WinForms.pdf
ManhHoangVan
 
PPT
ASP.NET Session 11 12
Sisir Ghosh
 
PPSX
06 asp.net session08
Vivek Singh Chandel
 
DOCX
Ado dot net complete meterial (1)
Mubarak Hussain
 
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
waisfarjam
 
PDF
14 22 size sql book(1)
bhganesh
 
PDF
Building better SQL Server Databases
ColdFusionConference
 
PPS
06 asp.net session08
Mani Chaubey
 
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
tvrsamj5492
 
Asp .Net Database Connectivity Presentation.pptx
sridharu1981
 
Synapseindia dot net development chapter 8 asp dot net
Synapseindiappsdevelopment
 
Ado Presentation
Rebecca Peltz
 
unit 3.docx
Sadhana Sreekanth
 
The sql connection object
Sharat Chandu
 
SQL Server with CSharp WinForms.pdf
Mona686896
 
For Beginers - ADO.Net
Snehal Harawande
 
Database connectivity in asp.net
baabtra.com - No. 1 supplier of quality freshers
 
Using sql server in c sharp
Faruk Alkan
 
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
samimylahji
 
04. SQL Servesdafr with CSharp WinForms.pdf
ManhHoangVan
 
ASP.NET Session 11 12
Sisir Ghosh
 
06 asp.net session08
Vivek Singh Chandel
 
Ado dot net complete meterial (1)
Mubarak Hussain
 
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
waisfarjam
 
14 22 size sql book(1)
bhganesh
 
Building better SQL Server Databases
ColdFusionConference
 
06 asp.net session08
Mani Chaubey
 
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
tvrsamj5492
 
Ad

Recently uploaded (20)

PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 

Database connectivity to sql server asp.net

  • 1. DATABASE CONNECTIVITY Programming : C# Technology : Asp.net Database : MS Sql Server Author : Hemant Sankhla (Web Developer)
  • 2. I will teach you how to connect MS Sql Server Database to an Asp.Net Web Application using C#. After the successful connection you are able to perform various operations on database from your web application like select, insert, update, delete etc. Lets check it out and do yourself. Author : Hemant Sankhla (Web Developer)
  • 3. First of all Create Database in MS Sql Server 1. Go to Database > Right Click > New Database. And give a name to your Database. 2. Now Create a table in this database. To do such select the database > Right click On Table > New Table. Now enter desired fields and save it using ctrl + s. Now your database with tables is ready to be a part of your web application. Author : Hemant Sankhla (Web Developer)
  • 4. Create The Connection String (Path to server and database) Connection string means the address to the Sql Server and The database using the security access, or if there is an username and password then providing these credentials for accessing the server and database. Now type below line in ‘web.config’ file which you have in your website directory, search for it in Solution Explorer. If we make connection in ‘web.config’ file then we only have to make it once and we call it where we want it. <connectionStrings> <add name = "con" connectionString="Data Source=.sqlexpress ; Initial Catalog=Database; Integrated Security=true"/> </connectionStrings> Author : Hemant Sankhla (Web Developer)
  • 5. Create The Connection String (Path to server and database) name : The name of your connection ConnectionString : The full path or address to your server and database DataSource : Sql Server Name (you can get this in Sql server management studio. Copy the server name from the connection dialog box at starting.) Initial Catalog : Your Database. Integrated Security : True, use this if you have no username and password in sql server. Author : Hemant Sankhla (Web Developer)
  • 6. Using NameSpaces Namespace is a collection of classes and methods. In C# various namespaces are used in different work. Here we want to connect and use the Sql Server so we have to use appropriate namespaces for this task. Namespaces are used by the ‘Using keyword.’ Add these two name spaces. using System.Data; using System.Data.SqlClient; Author : Hemant Sankhla (Web Developer)
  • 7. Using NameSpaces Namespace is a collection of classes and methods. In C# various namespaces are used in different work. Here we want to connect and use the Sql Server so we have to use appropriate namespaces for this task. Namespaces are used by the ‘Using keyword.’ Add these two name spaces. using System.Data; using System.Data.SqlClient; using System.Web.Configuration; Author : Hemant Sankhla (Web Developer)
  • 8. The Essential Code Below is the essential code for all type of operation. SqlConnection con = new SqlConnection (WebConfigurationManager.ConnectionStrings["con"].ToString( )); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; Author : Hemant Sankhla (Web Developer)
  • 9. Understanding The Code SqlConnection : This class creates a connection object. Con is the object for connection. WebConfigurationManager : This class fetch the connection string from the web.config file. Con.open( ) : Open the current connection. SqlCommand : Create the Sql command Query’s object. Cmd.connection = con : Supply the con object to the cmd connection. Author : Hemant Sankhla (Web Developer)
  • 10. Select All SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToStrin g()); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "select * from Table "; SqlDataAdapter da = new SqlDataAdapter(); DataSet ds = new DataSet(); da.SelectCommand = cmd; da.Fill(ds); Author : Hemant Sankhla (Web Developer)
  • 11. Insert Into SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToString()); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "insert into Student (FullName, Marks) values ('" + txtName.Text + "','" + txtMarks.Text + "')"; int h = cmd.ExecuteNonQuery(); if (h > 0) { Response.Write("Inserted Successfully"); } Author : Hemant Sankhla (Web Developer)
  • 12. Update Record SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToString()); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = “update Student SET FullName = '" + txtName.Text + "', Marks = '" + txtMarks.Text + “’ where ID = ID; int h = cmd.ExecuteNonQuery(); if (h > 0) { Response.Write(“Record Updated Successfully"); } Author : Hemant Sankhla (Web Developer)
  • 13. Delete Record SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToString()); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = “delete from Student where ID =‘ ” + ID + “ ‘; int h = cmd.ExecuteNonQuery(); if (h > 0) { Response.Write(“Record Deleted Successfully"); } Author : Hemant Sankhla (Web Developer)
  • 14. Now you have to practice those codes by hand. If you feel any trouble comment me These codes also work well in C# Windows Desktop Application. Author : Hemant Sankhla (Web Developer)