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)

View
ViewView
View
LakshmiSamivel
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
arpit bhadoriya
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQL
Pooja Dixit
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 
SQL
SQLSQL
SQL
Vineeta Garg
 
Java Strings
Java StringsJava Strings
Java Strings
RaBiya Chaudhry
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
Gowarthini
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
Shakila Mahjabin
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
Ashim Lamichhane
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
Sachidananda M H
 
MYSQL Aggregate Functions
MYSQL Aggregate FunctionsMYSQL Aggregate Functions
MYSQL Aggregate Functions
Leroy Blair
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
Eryk Budi Pratama
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
Arun Sial
 
Java script
Java scriptJava script
Java script
Shyam Khant
 
Crystal report
Crystal reportCrystal report
Crystal report
baabtra.com - No. 1 supplier of quality freshers
 
Database Connection
Database ConnectionDatabase Connection
Database Connection
John Joseph San Juan
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Microsoft SQL Server Database Administration.pptx
Microsoft SQL Server Database Administration.pptxMicrosoft SQL Server Database Administration.pptx
Microsoft SQL Server Database Administration.pptx
samtakke1
 

Viewers also liked (20)

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

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

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

Recently uploaded (20)

IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdfIntroduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptxSPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive LearningSustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Revista digital preescolar en transformación
Revista digital preescolar en transformaciónRevista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdfIntroduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptxSPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive LearningSustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Revista digital preescolar en transformación
Revista digital preescolar en transformaciónRevista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 

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)