Click here to Skip to main content
15,919,613 members
Articles / Database Development / SQL Server
Article

The Base Class of Connecting To Database

Rate me:
Please Sign up or sign in to vote.
1.30/5 (7 votes)
25 Sep 20052 min read 24.6K   274   18   4
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--------------------

 

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
China China
About me? Do you wanna know these? No, it's useless for you, wow!Smile | :)
For more than four years I have engaged in the field of IT (In chinese words which may say "Ai Ti" in PinYin, means a behavior of someone who was kicked by others, so funny, right?), always being a software engineer. At the beginning, my alone role of career is staying arround the surrounding of China TELCOM, now changes have taken place, that is, I have discarded itSmile | :) So cool, man! I aimed to the next step where I go further...........

Comments and Discussions

 
Generalcan't connect to database Pin
galroy26-Sep-05 5:32
professionalgalroy26-Sep-05 5:32 
i create access 2000 db and try to connect with your class but the connection Failed.
Any idea why?
GeneralRe: can't connect to database Pin
Daniel Moszczanski5-Jan-06 3:05
Daniel Moszczanski5-Jan-06 3:05 
GeneralRe: can't connect to database Pin
Churn Wuang10-Jan-06 20:12
Churn Wuang10-Jan-06 20:12 
GeneralRe: can't connect to database Pin
Churn Wuang10-Jan-06 20:02
Churn Wuang10-Jan-06 20:02 

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.