Professional Documents
Culture Documents
Visual Studio .NET provides access to databases through the set of tools and
namespaces collectively referred to as Microsoft ADO.NET
Why will you usually create an ASPNET user account in the Database for an
ASP.NET web application?
Web applications run using the ASPNET user account. The SQL database
administrator will have to set up this account and grant it permissions before your
Web application will have access to a SQL database. For file-based databases, such
as Microsoft Access, you must grant permissions on the database file to the ASPNET
user account using Windows file security settings.
How do you ensure that the database connections are always closed?
To ensure that the database connections are always closed, open the connection
inside of a using block, as shown in the following code fragment. Doing so ensures
that the connection is automatically closed when the code exits the block.
using (SqlConnection ConnectionObject = new SqlConnection())
{
ConnectionObject.Open();
//The database connection will be closed when the control exits the using code
block
}
What are the methods that can ensure asynchronous execution of the
Transact-SQL statement or stored procedure?
BeginExecuteNonQuery
BeginExecuteReader
How do you get the total number of columns in the current row of a
SqlDataReader instance?
FieldCount property can be used to get the total number of columns in the current
row of a SqlDataReader instance.
How do you retrieve two tables of data at the same time by using data
reader?
Include 2 select statements either in a stored procedure or in a select command
and call the ExecuteReader() method on the command object. This will
automatically fill the DataReader with 2 Tables of data.
The datareader will always return the data from first table only. If you want to get
the second table then you need to use ReaderObject.NextResult() method. The
NextResult() method will return true if there is another table. The following code
shows you how do it.
//Create the SQL Query with 2 Select statements
string SQLQuery = "Select * from Customers;Select * from Employees;";
//Create the Connection Object
SqlConnection ConnectionObject = new SqlConnection(ConnectionString);
//Create the Command Object
SqlCommand CommandObject = new SqlCommand(SQLQuery, ConnectionObject);
//Open the connection
ConnectionObject.Open();
//Execute the command. Now reader object will have 2 tables of data.
SqlDataReader ReaderObject = CommandObject.ExecuteReader();
//Loop thru the tables in the DataReader object
while (ReaderObject.NextResult())
{
while (ReaderObject.Read())
{
//Do Something
}
}
//Close the Reader
ReaderObject.Close();
//Close the Connection
ConnectionObject.Close();
What are the advantages of using SQL stored procedures instead of adhoc
SQL queries in an ASP.NET web application?
Better Performance : As stored procedures are precompiled objects they execute
faster than SQL queries. Every time we run a SQL query, the query has to be first
compiled and then executed where as a stored procedure is already compiled.
Hence executing stored procedures is much faster than executing SQL queries.
Better Security: For a given stored procedure you can specify who has the rights
to execute. You cannot do the same for an SQL query. Writing the SQL statements
inside our code is usually not a good idea. In this way you expose your database
schema (design) in the code which may be changed. Hence most of the time
programmers use stored procedures instead of plain SQL statements.
Reduced Network Traffic : Stored Procedures reside on the database server. If
you have to execute a Stored Procedure from your ASP.NET web application you
just specify the name of the Stored Procedure. So over the network you just send
the name of the Stored Procedure. With an SQL query you have to send all the SQL
statements over the network to the database server which could lead to increased
network traffic.