3. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
4. How to connect an asp.net application
with Database
• ConnectionString:-
The database connection information is stored
in the web.configfile as a ConnectionString
tag.
•Web.configis a xml file which is used to store
application wide settings and constants
5. How to connect an asp.net
application with Database
•The ConnectionString has two main attributes
Name:–this holds a name for the
connectionstring with which a particular
connectionstringis identified, useful when
using multiple databases
ConnectionString–this holds the actual
connection data
6.
7. Getting the connection information
from within the application
• The connection string added in the web.configcan be obtained from
anywhere within the application using the following line of code
String
constring=configurationManager.ConnectionString["connection"].con
nectionString;
ConnectionStringname as given in the web.config
8. Cont….
• For connecting to the database from
anasp.net application we use
the following inbuilt classes of the
.netframework
-sqlCommandobject
-sqlConnectionobject
-sqlDataReader/ sqlDataAdapterobject
9. Creating and initializing the
sqlCommandobject
• The following lines of code creates an sqlCommandobject and
initializes it
string conString =
ConfigurationManager.ConnectionStrings["connection"].ConnectionStri
ng;
SqlConnection conn = new SqlConnection(conString);
string query="insert into tbl_user
values('"+txtname.Text+"','"+txtpassword.Text+"','"+txtaddress.Text+"')";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
10. Sytax for Data Retrieve
string conString =
ConfigurationManager.ConnectionStrings["connection"].Conn
ectionString;
SqlConnection con = new SqlConnection(conString);
string query = "select * from tbl_user where
vchr_username='" +txtlogname.Text+ "' and vchr_password='"
+ txtlogpass.Text + "'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
12. Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
13. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
14. Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us