Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom Data Layer for ASP.NET With MySQL

0.00/5 (No votes)
13 Dec 2007 1  
Data access layer for MySQL based web projects.

Introduction

Have you ever used a vague combination such as ASP.NET (2.0) and a database like MySQL? In this project, you would get data access functionality for MySQL that would help you to minimize your efforts to write a Data Access Layer in your Web Projects while using such a combination.

Background

The MySql.Data namespace consists most of the required classes. This namespace have some methods in the classes which do not accommodate all features such as ExecuteDataset, and some of them, say, MySqlHelper, do not support Stored Procedures with parameters. In this project, the data access layer would contain methods such as ExecuteDataset, ExecuteNonQuery, ExecuteScalar, and ExecuteReader, which would use Stored Procedures and queries, both with and without parameters.

Using the code

Here is how to use the data access class in the code. For detailed implementation, please go through the demo project. Set the connection string appropriately in the web.config file.

/// This method demonstrates ExecuteDataset.
/// Passing CommandType as Store Procedure & CommandText
/// as Store Procedure Name.

DataSet dsEmployee = new DataSet();
dsEmployee = DataAccess.ExecuteDataSet(CommandType.StoredProcedure, 
                        "EmployeeDetails");

/// This method demonstrates ExecuteDataset.
/// Passing CommandType as Store Procedure & CommandText
/// as Store Procedure Name And Parameters.

DataSet dsEmployee = new DataSet();

MySqlParameter[] mySqlParams = new MySqlParameter[1];
mySqlParams[0] = new MySqlParameter();
mySqlParams[0].ParameterName = "?EmpId";
mySqlParams[0].Value = 2;

dsEmployee = DataAccess.ExecuteDataSet(CommandType.StoredProcedure, 
             "EmployeeDetails",mySqlParams);

/// This method demonstrates ExecuteDataset.
/// Passing CommandType as Text & CommandText as Query And Parameters.

DataSet dsEmployee = new DataSet();

MySqlParameter[] mySqlParams = new MySqlParameter[1];
mySqlParams[0] = new MySqlParameter();
mySqlParams[0].ParameterName = "?EmpId";
mySqlParams[0].Value = 2;

dsEmployee = DataAccess.ExecuteDataSet(CommandType.Text, 
  "Select * from employee where Id =?EmpId", mySqlParams);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here