Click here to Skip to main content
15,888,340 members
Articles / Web Development / ASP.NET
Article

Data Access Helper Class

Rate me:
Please Sign up or sign in to vote.
1.24/5 (19 votes)
9 Oct 2003 76.6K   22   4
This class helps with connecting to DB engine and with returning of resultsets.

Introduction

This is database access helper class with easy usage. For more information about designing data access helper classes see section Designing data layers in Application Architecture for .NET: Designing Applications and Services

Requirements

Only one thing you have to do before using this class, is to store your ADO connection string into the web.config file.

Usage

C#
public class DBAudit
{
 public static DataSet TextInfo(string IdCat, string IdTopic, string Lang, Guid IdObject)
 {
  return DB.FillDataset("STORED_PROC_NAME",
   new SqlParameter[] {
        new SqlParameter("@ID_CAT",SqlDbType.VarChar, 8),
        new SqlParameter("@ID_TOPIC",SqlDbType.VarChar, 8),
        new SqlParameter("@LANG",SqlDbType.VarChar, 8),
        new SqlParameter("@ID_OBJEct",SqlDbType.UniqueIdentifier, 16)
     },
   new object[] {IdCat, IdTopic, Lang, IdObject});
 }
}


Source code

C#
namespace Infinity.DB
{
 /// <summary>
 /// Common methods for DB connection
 /// </summary>
 public class DB
 {
  const string DBConnStr = "DBSTR";

  /// <summary>
  /// Returns ADO connection string
  /// </summary>
  public static string GetConnStr()
  {
   return ConfigurationSettings.AppSettings[DBConnStr].ToString();
  }

  /// <summary>
  /// Returns filled dataset from stored procedure name and its parameters
  /// </summary>
  public static DataSet FillDataset(string SProc, SqlParameter[] Params, object[] Values)
  {
   return FillDataset(SProc, Params, Values, DB.GetConnStr());
  }

  /// <summary>
  /// Returns filled dataset from stored procedure name and its parameters
  /// </summary>
  public static DataSet FillDataset(string SProc, SqlParameter[] Params, object[] Values, string ConStr)
  {
   SqlConnection myConnection = new SqlConnection(ConStr);
   SqlDataAdapter myAdapter = new SqlDataAdapter();

   myAdapter.SelectCommand = new SqlCommand(SProc, myConnection);
   myAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

   // assign all parameters with its values
   for(int i = 0; i < Params.Length; i++)
   {
    myAdapter.SelectCommand.Parameters.Add(Params[i]).Value = Values[i];
   }

   DataSet myDataSet = new DataSet();

   myAdapter.Fill(myDataSet);

   return myDataSet;
  }

  /// <summary>
  /// Executes stored procedure with its parameters
  /// </summary>
  public static void ExecSQL(string SProc, SqlParameter[] Params, object[] Values)
  {
   SqlConnection myConnection = new SqlConnection(DB.GetConnStr());
   SqlCommand myCmd = new SqlCommand(SProc, myConnection);
   myCmd.CommandType = CommandType.StoredProcedure;

   // assign all parameters with its values
   for(int i = 0; i < Params.Length; i++)
   {
    myCmd.Parameters.Add(Params[i]).Value = Values[i];
   }

   myConnection.Open();
   myCmd.ExecuteNonQuery();
   myConnection.Close();
  }
 }
}

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


Written By
Web Developer
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalwhat Pin
Anonymous22-Oct-04 12:56
Anonymous22-Oct-04 12:56 
GeneralAn other sample Pin
Anonymous17-Oct-03 15:53
Anonymous17-Oct-03 15:53 
Generalpoor Pin
Anonymous10-Oct-03 10:46
Anonymous10-Oct-03 10:46 
GeneralRe: poor Pin
Radim_Hampel13-Oct-03 4:24
Radim_Hampel13-Oct-03 4:24 

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.