Click here to Skip to main content
15,879,326 members
Articles / Web Development / ASP.NET

Custom Data Layer for ASP.NET With MySQL

Rate me:
Please Sign up or sign in to vote.
4.40/5 (7 votes)
13 Dec 2007CPOL 46.7K   2K   38   4
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.

C#
/// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Having 3.5 years of work experience in software development.

Good knowledge of software development lifecycle, active involvement in development, testing and support.

Earned several Dot Net certified credentials from Brian Bench and Microsoft like MCPD, MCTS MCAD.

Comments and Discussions

 
QuestionHi Pin
shriram20106-Sep-10 2:12
shriram20106-Sep-10 2:12 
Ranthi Pin
cyanobacteria12-May-08 9:21
cyanobacteria12-May-08 9:21 
GeneralRe: hi Pin
Shivanand Alagi23-Apr-09 21:11
Shivanand Alagi23-Apr-09 21:11 
GeneralRe: hi Pin
Vivek Thakur25-May-09 9:07
professionalVivek Thakur25-May-09 9:07 
can i have your email id? please email me at : vivekthakur(at) gmail.com

http://www.vivekthakur.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.