asp.net.ph

Skip Navigation LinksHome > Data Access in Web Forms > Connecting to a Database > Calling Data Functions in Code Behind

Calling Data Functions in Code Behind

Securing Connection Strings   Displaying Information From a Database


So far, we have covered in brief the core ADO.NET functionalities needed to implement data access in Web pages.

  • Connections - for connecting to and managing operations against a database.
  • Commands - for issuing SQL commands to execute against a database.
  • DataReaders - for directly reading data off a stream.
  • DataSets and DataAdapters - for storing and manipulating data in a memory-resident data store.

You may have already noticed that each time a page needs data access, you need to code the sequence each time to gain acess to a data store.

There is a much more efficient way, though, apart from using data source controls, by coding these data functions in an external script file.

This way, the functions in the script can just be called by any page anytime data acess is needed, without having to rewrite the connect, command, and get data into a reader or dataset sequence.

This workshop makes extensive use of this method, using only two files for data access: one for SQL Server and one for OleDb databases.

NOTE: The discussion here assumes that connection strings are stored in the application’s web.config file. If not, please see Securing Connection Strings.

The following examples demonstrate use of this concept.

Calling Data Functions in Code Behind (SQL)
Run Sample | View Source
Calling Data Functions in Code Behind (OleDb)
Run Sample | View Source

Basically, both files share the same logic, each having the following reusable methods:

  • generic method to fetch data into a reader - fetchReader ( )
  • generic method to fetch data into a dataset - fetchData ( )
  • generic method to fetch a scalar value - fetchScalar ( )

In addition, the script for SQL Server data includes a getConnection method for establishing a connection to a named connection string in the web.config file.

For access to SQL Server data, the script uses classes in the System.Data.SqlClient namespace, and is named fetchData_sql.cs.

For access to OleDb data, the script uses classes in the System.Data.OleDb namespace, and is named fetchData_oledb.cs.

And to invoke, simply add a reference to the appropriate script file in any page that may need it. For example:

To connect to and execute commands against an SQL Server database:

<script language="C#" runat="server" src="~/shared/fetchData_sql.cs" />

To connect to and execute commands against an OleDb database:

<script language="C#" runat="server" src="~/shared/fetchData_oledb.cs" />
Calling Data Functions in Code Behind (SQL)
Run Sample | View Source
Calling Data Functions in Code Behind (OleDb)
Run Sample | View Source
See Also

Securing Connection Strings   Displaying Information From a Database



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note