SlideShare une entreprise Scribd logo
1  sur  14
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)

Contenu connexe

Tendances (20)

Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Java script
Java scriptJava script
Java script
 
Building beautiful apps with Google flutter
Building beautiful apps with Google flutterBuilding beautiful apps with Google flutter
Building beautiful apps with Google flutter
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUX
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
WPF
WPFWPF
WPF
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
C#.NET
C#.NETC#.NET
C#.NET
 
Software architectural patterns - A Quick Understanding Guide
Software architectural patterns - A Quick Understanding GuideSoftware architectural patterns - A Quick Understanding Guide
Software architectural patterns - A Quick Understanding Guide
 
Windows form application - C# Training
Windows form application - C# Training Windows form application - C# Training
Windows form application - C# Training
 
Csv file read and write
Csv file read and writeCsv file read and write
Csv file read and write
 
Standard control in asp.net
Standard control in asp.netStandard control in asp.net
Standard control in asp.net
 
Python my sql database connection
Python my sql   database connectionPython my sql   database connection
Python my sql database connection
 
Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
 
COMPUTER GRAPHICS PROJECT REPORT
COMPUTER GRAPHICS PROJECT REPORTCOMPUTER GRAPHICS PROJECT REPORT
COMPUTER GRAPHICS PROJECT REPORT
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.net
 
Flutter presentation.pptx
Flutter presentation.pptxFlutter presentation.pptx
Flutter presentation.pptx
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 

En vedette

13206532 pss7
13206532 pss713206532 pss7
13206532 pss7Rubyming
 
Bluetooth - Replacement Of Wires
Bluetooth - Replacement Of WiresBluetooth - Replacement Of Wires
Bluetooth - Replacement Of WiresHemant 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 @EtixLabsStephane Gleizes
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolRandy 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 concurrencyxu liwei
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance TopicsAli 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 UpadhyayBipin Upadhyay
 
HTTP Protocol Basic
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol BasicChuong 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++11Uilian 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 IPv6Rodolfo 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 managementShivanand Arur
 

En vedette (20)

13206532 pss7
13206532 pss713206532 pss7
13206532 pss7
 
Bluetooth - Replacement Of Wires
Bluetooth - Replacement Of WiresBluetooth - Replacement Of Wires
Bluetooth - Replacement Of Wires
 
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...
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP Protocol
 
TCP/IP
TCP/IPTCP/IP
TCP/IP
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance Topics
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
"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
 
HTTP Protocol Basic
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol Basic
 
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)/
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
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
 
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
 
ASP.NET State management
ASP.NET State managementASP.NET State management
ASP.NET State management
 

Similaire à Database connectivity to sql server asp.net

Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.netNgeam Soly
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Adooswchavez
 
CIS407AWk2iLabDefault.aspx Greetings and Salutations.docx
CIS407AWk2iLabDefault.aspx        Greetings and Salutations.docxCIS407AWk2iLabDefault.aspx        Greetings and Salutations.docx
CIS407AWk2iLabDefault.aspx Greetings and Salutations.docxclarebernice
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPAli Shah
 
Database By Salman Mushtaq
Database By Salman MushtaqDatabase By Salman Mushtaq
Database By Salman MushtaqSalman Mushtaq
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShakir Majeed Khan
 

Similaire à Database connectivity to sql server asp.net (20)

Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
Mvc acchitecture
Mvc acchitectureMvc acchitecture
Mvc acchitecture
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
Ado.net
Ado.netAdo.net
Ado.net
 
2310 b 10
2310 b 102310 b 10
2310 b 10
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Ado
 
CIS407AWk2iLabDefault.aspx Greetings and Salutations.docx
CIS407AWk2iLabDefault.aspx        Greetings and Salutations.docxCIS407AWk2iLabDefault.aspx        Greetings and Salutations.docx
CIS407AWk2iLabDefault.aspx Greetings and Salutations.docx
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Lecture14
Lecture14Lecture14
Lecture14
 
Ado Presentation
Ado PresentationAdo Presentation
Ado Presentation
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
 
Database By Salman Mushtaq
Database By Salman MushtaqDatabase By Salman Mushtaq
Database By Salman Mushtaq
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbs
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdf
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 

Dernier

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Dernier (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

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)