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

PPSX
ADO.NET
PPTX
Database connectivity in asp.net
PPT
SQL Queries
PPTX
Classes objects in java
PPT
Introduction to ADO.NET
PPT
ASP.NET 10 - Data Controls
PPTX
Ado.Net Tutorial
PPTX
SQL Queries Information
ADO.NET
Database connectivity in asp.net
SQL Queries
Classes objects in java
Introduction to ADO.NET
ASP.NET 10 - Data Controls
Ado.Net Tutorial
SQL Queries Information

What's hot (20)

PPT
MySQL and its basic commands
PPT
Database Connection
PPTX
Sql subquery
PPT
PPT
SQL subquery
PDF
Chapter 4 Structured Query Language
PPT
Asp.net.
PPTX
Ch 7 data binding
PPTX
VB.NET:An introduction to Namespaces in .NET framework
PPT
Oops concepts in php
PPTX
Strings in Java
PDF
Nested Queries Lecture
PDF
Asp.net state management
PPTX
SQL Commands
PPTX
SQL - Structured query language introduction
PPTX
SQL commands
PPT
ADO .Net
PPTX
Java database connectivity with MySql
MySQL and its basic commands
Database Connection
Sql subquery
SQL subquery
Chapter 4 Structured Query Language
Asp.net.
Ch 7 data binding
VB.NET:An introduction to Namespaces in .NET framework
Oops concepts in php
Strings in Java
Nested Queries Lecture
Asp.net state management
SQL Commands
SQL - Structured query language introduction
SQL commands
ADO .Net
Java database connectivity with MySql
Ad

Viewers also liked (20)

PPTX
13206532 pss7
PPTX
Bluetooth - Replacement Of Wires
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
PDF
C++17 introduction - Meetup @EtixLabs
PDF
Web I - 05 - HTTP Protocol
PDF
Modern c++ (C++ 11/14)
PDF
Bjarne essencegn13
PPTX
PDF
C++11
PDF
C++11 concurrency
PPTX
Asp Net Advance Topics
PDF
C++11 & C++14
PDF
"Http protocol and other stuff" by Bipin Upadhyay
PPTX
HTTP Protocol Basic
PDF
C++17 - the upcoming revolution (Code::Dive 2015)/
PPTX
Elements of C++11
PDF
Networking - TCP/IP stack introduction and IPv6
PDF
C++11 Idioms @ Silicon Valley Code Camp 2012
PPTX
ASP.NET State management
13206532 pss7
Bluetooth - Replacement Of Wires
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C++17 introduction - Meetup @EtixLabs
Web I - 05 - HTTP Protocol
Modern c++ (C++ 11/14)
Bjarne essencegn13
C++11
C++11 concurrency
Asp Net Advance Topics
C++11 & C++14
"Http protocol and other stuff" by Bipin Upadhyay
HTTP Protocol Basic
C++17 - the upcoming revolution (Code::Dive 2015)/
Elements of C++11
Networking - TCP/IP stack introduction and IPv6
C++11 Idioms @ Silicon Valley Code Camp 2012
ASP.NET State management
Ad

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

PPTX
PPTX
Asp .Net Database Connectivity Presentation.pptx
PPT
Synapseindia dot net development chapter 8 asp dot net
ODP
Ado Presentation
DOCX
unit 3.docx
PPTX
The sql connection object
PDF
SQL Server with CSharp WinForms.pdf
PPT
For Beginers - ADO.Net
PDF
Using sql server in c sharp
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
PDF
04. SQL Servesdafr with CSharp WinForms.pdf
PPT
ASP.NET Session 11 12
PPSX
06 asp.net session08
DOCX
Ado dot net complete meterial (1)
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
PDF
14 22 size sql book(1)
PDF
Building better SQL Server Databases
PPS
06 asp.net session08
PDF
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
Asp .Net Database Connectivity Presentation.pptx
Synapseindia dot net development chapter 8 asp dot net
Ado Presentation
unit 3.docx
The sql connection object
SQL Server with CSharp WinForms.pdf
For Beginers - ADO.Net
Using sql server in c sharp
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
04. SQL Servesdafr with CSharp WinForms.pdf
ASP.NET Session 11 12
06 asp.net session08
Ado dot net complete meterial (1)
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...
14 22 size sql book(1)
Building better SQL Server Databases
06 asp.net session08
Programming Microsoft SQL Server 2000 with Microsoft Visual Basic NET 1st edi...

Recently uploaded (20)

PDF
Physical pharmaceutics two in b pharmacy
PPT
hsl powerpoint resource goyloveh feb 07.ppt
DOCX
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PPTX
MMW-CHAPTER-1-final.pptx major Elementary Education
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
PPTX
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
PDF
FAMILY PLANNING (preventative and social medicine pdf)
PPTX
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
PDF
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
PDF
GSA-Past-Papers-2010-2024-2.pdf CSS examination
PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PPTX
climate change of delhi impacts on climate and there effects
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
PDF
African Communication Research: A review
PPTX
GW4 BioMed Candidate Support Webinar 2025
Physical pharmaceutics two in b pharmacy
hsl powerpoint resource goyloveh feb 07.ppt
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
ACFE CERTIFICATION TRAINING ON LAW.pptx
MMW-CHAPTER-1-final.pptx major Elementary Education
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
FAMILY PLANNING (preventative and social medicine pdf)
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
Theoretical for class.pptxgshdhddhdhdhgd
GSA-Past-Papers-2010-2024-2.pdf CSS examination
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
Diabetes Mellitus , types , clinical picture, investigation and managment
climate change of delhi impacts on climate and there effects
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
faiz-khans about Radiotherapy Physics-02.pdf
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
African Communication Research: A review
GW4 BioMed Candidate Support Webinar 2025

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)