65.9K
CodeProject is changing. Read more.
Home

The Base Class of Connecting To Database

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.30/5 (7 votes)

Sep 25, 2005

2 min read

viewsIcon

24750

downloadIcon

274

Perfect interface design for common database which would be used to easily connect to the most kinds of database, such as MS Access, MS SQL Server,..., and so on. Do not worry again about the terrible complex interface of ADO COM, take it easy to implementing your operation logical codes...

Introduction

  It's very earlier that I have terrible with ADO, because of its foolish mass interface usage. So comfidences with me to do with it at all, periods of that unimagination nightmare passed at this time. I always believe that everything is determined by someting brief and something easier, so that I have to re-design the user interface with ADO. Everything is easy to do! Simple CONNECT, simple DISCONNECT, simple EXECUTE, simple GET_FIELDVALUE,..., and simple extension and maintenance for you everyone, the behaviours all are explicit and clearer, you don't card the mass type of COM again. In all words, cool!

  Welcome you to revise them as you can, please remember our aim is "Everything is simple, simple is perfect".

----------------SAMPLE AS BELOW-----------------

void AccessDB ()
{
 TField oField;
 long lRecordCount = 0;
 
 LPCSTR lpDataSource = "D:\\userdata.MDB";
 LPCSTR lpProvider = "Microsoft.Jet.OLEDB.4.0";
 LPCSTR lpUser = "";
 LPCSTR lpPassword = "SayHelloToYou";
 TDataBase database (lpDataSource, lpProvider, lpUser, lpPassword);
 if (!database.Connect ())
 {
  TRACE ("Failed to connect to DB server!\n %s\n", database.GetErrorMessage ());
  return;
 }

 if (!database.ExecSQL ("SELECT Field1, Field2, Field3, Field4 FROM TableToAccess"))
 {
  TRACE ("Failed to execute SQL scripts!\n %s\n", database.GetErrorMessage ());
  goto FinalDo;
 }

 if (database.IsEmpty ())
  goto FinalDo;

// lRecordCount = database.GetRecordCount ();
// TRACE ("The number of record=%d\n", lRecordCount);

 if (!database.First())
  TRACE ("Failed to move to first\n");

 while (!database.Eof())
 {
  if (database.GetFieldByName ("Field1", oField))
   TRACE ("name:Field1=%s\n", (oField.IsNULL) ? "<NULL>" : oField.StringValue);
  if (database.GetFieldByIndex (0, oField))
   TRACE ("index:Field1=%s\n", (oField.IsNULL) ? "<NULL>" : oField.StringValue);

  if (database.GetFieldByName ("Field2", oField))
   TRACE ("name:Field2=%u\n", (oField.IsNULL) ? 0xFFFFFFFF : oField.IntValue);
  if (database.GetFieldByIndex (1, oField))
   TRACE ("index:Field2=%u\n", (oField.IsNULL) ? 0xFFFFFFFF : oField.IntValue);

  if (database.GetFieldByName ("Field3", oField))
   TRACE ("name:Field3=%s\n", (oField.IsNULL) ? "<NULL>" : oField.DateTimeValue.Format ("%Y-%m-%d"));
  else
   TRACE ("%s\n", database.GetErrorMessage());
  if (database.GetFieldByIndex (2, oField))
   TRACE ("index:Field3=%s\n", (oField.IsNULL) ? "<NULL>" : oField.DateTimeValue.Format ("%Y-%m-%d"));

  if (database.GetFieldByName ("Field4", oField))
   TRACE ("name:Field4=%.2e\n", (oField.IsNULL) ? 0.00 : oField.DoubleValue);
  if (database.GetFieldByIndex (3, oField))
   TRACE ("index:Field4=%.2e\n", (oField.IsNULL) ? 0.00 : oField.DoubleValue);
  

  if (!database.Next ())
   TRACE ("Failed to move to next\n");
 }
 
FinalDo:
 database.DisConnect (); 
}

 

--------------------END OF SAMPLE--------------------