2. How to connect an asp.net application with Database
ConnectionString
• The database connection information is stored in the web.config
file as a ConnectionString tag.
• Web.config is a xml file which is used to store application wide
settings and constants
• The ConnectionString has two main attributes
– Name – this holds a name for the connectionstring with which a
particular connectionstring is identified, useful when using multiple
databases
– ConnectionString – this holds the actual connection data
4. Getting the connection information from within the application
The connection string added in the web.config can be obtained from
anywhere within the application using the following line of code
ConnectionString name as given in the web.config
For connecting to the database from an asp.net application we use
the following inbuilt classes of the .net framework
• sqlCommand object
• sqlConnection object
• sqlDataReader / sqlDataAdapter object
5. Creating and initializing the sqlCommand object
The following lines of code creates an sqlCommand object and initializes it
Name of the
stored
procedure
passed as an
argument
Setting the command type property
as stored procedure, the other
commandtypes are text(for inline
queries) and table direct
Setting the commandName property
to the passed in stored procedure
name
The above function takes the name of a stored procedure as parameter and
returns a sqlCommand object whose commandtype is set as stored procedure
and commandText is set as the name of the stored procedure
6. Interacting with the database
The following lines of code connects with the database and runs the query in the
commandText property of the sqlCommand object in the database
property of the sqlCommand object The sqlCommand object is
passed as an argument to
the function
sqlConnection object is
initialized with the data in
the connectionstring
sqlDataAdapter object is
initialized for the
sqlCommand object
Interacts with the
database and returns the
resulting dataset
A new dataset variable is
initialized for holding the
returning data from the
database (if any)
7. Interacting with the database
• sqlDataReader object can also be used to interact with the database but it
allows only one way communication, that is, read from the database, while
sqlDataAdapter object allows writing to as well as reading from the database
• The data read from the database is stored in the dataset object and returned
Retrieving data from the dataset object
A dataset object is a collection of datatable objects, datatables contain
datarows and columns just as it will appear in a database table
For eg. Suppose the dataset returned by a certain database interaction is as
follows
Id Name
10 Ab
11 cd
class Subject
v Maths
x English
Table 1 Table 2
ds
8. Retrieving data from the dataset object
• The data in the second column of the first row in table 1 can be
retrieved as
– ds.Tables(0).rows(0)(1) or ds.Tables(0).rows(0)(“Name”)
• Similarly the data in the second column of the second row in table
2 can be retrieved as
– ds.Tables(1).rows(1)(1) or ds.Tables(1).rows(1)(“Subject”)
Id Name
10 Ab
11 cd
class Subject
v Maths
x English
Table 1 Table 2
9. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com