Click here to Skip to main content
Licence 
First Posted 9 Oct 2003
Views 59,230
Bookmarked 22 times

Data Access Helper Class

By | 9 Oct 2003 | Article
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

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

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

About the Author

Radim_Hampel

Web Developer

Czech Republic Czech Republic

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalwhat PinsussAnonymous12:56 22 Oct '04  
GeneralAn other sample PinsussAnonymous15:53 17 Oct '03  
Generalpoor PinsussAnonymous10:46 10 Oct '03  
GeneralRe: poor PinmemberRadim_Hampel4:24 13 Oct '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 10 Oct 2003
Article Copyright 2003 by Radim_Hampel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid