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

Simple ODBC classes without MFC

Rate me:
Please Sign up or sign in to vote.
3.93/5 (12 votes)
3 Nov 2001 127.4K   2.6K   35   12
A collection of simple classes to connect to ODBC capable DBMS and share those connections
  • Download demo project - 20 Kb
  • Introduction

    I have written a lot of database apps. Most of them have been small, and I soon realized that the CRecordset class was the only class requiring MFC for the majority of those apps. So, I sat down and wrote these classes to provide most of the functionality I needed and so I could get rid of MFC dependancies.

    These classes are extremely basic, and provide little use for someone who does not use the ODBC API directly. However, for those of you that do use the API, you might find them useful.

    There are three classes in this collection, CODBCCnx, CODBCQuery and CODBCFatQuery (you can find them all in the files ODBC.h and ODBC.cpp.) CODBCCnx is the most complex (so far), it creates the connections to the DBMS and allows you to easily share those connections. CODBCQuery and CODBCFatQuery really don't do much yet, but sometime in the near future I plan on adding functionality to them. For now, they are basically coming along for the ride, because they are in the same files ;)

    CODBCCnx class

    CODBCCnx provides three functions, Connect(), Disconnect() and IsOk(). The Connect() function allows you to connect either through a DSN or by using the computer's network name.

    //connecting to a DBMS through a DSN, "foodsn"<br>
    CODBCCnx dsn();
    dsn.Connect("foodsn","username","password");
    
    //NOTE: CODBCCnx dsn("foodsn",username","password"); is the same as above
    
    //checking to see if we are connected
    if(!dsn.IsOk()){
    	//we are connected
    }
    else{
    	//we are NOT connected
    }
    
    //connecting using a computer name on the network
    CODBCCnx name("SQL Server","CompName","username","password");
    //again, checking to see if we are connected
    if(!name.IsOk()){
    	//we are connected, and there was much rejoicing
    }
    else{
    	//not connected
    }

    Ok so now we are connected, lets disconnect from the database. The Disconnect() function forces you to make a decision on how you want to disconnect. Because there might possibly be transactions still going on, you can either roll them back, or wait for them to commit. If you do not provide a value for the parameter, calling Disconnect rolls the transactions back by default.

    //disconnecting from above connection (dsn)
    dsn.Disconnect();	//this rolls all uncommitted transactions back
    //NOTE: dsn.Disconnect(SQL_ROLLBACK) is the same thing
    <br>
    //disconnecting from above connection, name
    name.Disconnect(SQL_COMMIT); //this forces all uncommitted transactions to finish.

    CODBCQuery class

    Ok, so we have connected and disconnected, big deal. The nice thing about these classes is being able to share that connection we just made. This is where CODBCQuery comes in. CODBCQuery is not really useful by itself, but it is quite a handy class if you use it as a base class. The only thing note worthy about it, is the constructor, and the public statement handle. Code speaks well....

    //establish a connection using dsn
    CODBCCnx *pDsn = new CODBCCnx("foodsn", "username", "password");
    CODBCQuery qry1, qry2;
    
    if(!pDsn->IsOk())
    {
    	//connected!! now lets share some connections :)
    	pDsn->Connect(qry1.h_qstmt);
    	pDsn->Connect(qry2.h_qstmt);
    }
    else{
    	//not connected
    }
    
    //or we can do everything in one step
    CODBCQuery qry(new CODBCCnx("foodsn", "username", "password"), true);
    
    //or we can take the pDsn and pass it to a bunch of new query objects
    CODBCQuery qry2(pDsn), qry3(pDsn,false);
    
    //or we can have the CODBCQuery class connect itself
    CODBCQuery qry4("SQL Server", "CompName", "username", "password");
    //using a DSN
    CODBCQuery qry5(NULL, "foodsn", "username", "password");

    As you can see in the above code snippet, the constructors for CODBCQuery are pretty flexible. The most important thing to note is the boolean flag. If you pass the value true, CODBCQuery will delete the pointer. However, if you pass just the pointer or false as well, then CODBCQuery will NOT delete the pointer.

    One final note. There is a global function, SQLSuccess(). You pass it a SQLRETURN variable, and it will return a non-zero value if the SQLRETURN value is either SQL_SUCCESS or SQL_SUCCESS_WITH_INFO I just simply grew tired of typing those out ;)

    The only thing left to do after all this connecting and sharing is to bind variables to result sets. Which I leave for you to do :)

    I didn't provide a demo, because I feel that these classes are too simple to warrant one. However, if you would like one give me a holler and I will gladdly provide you with one.

    Dependancies

    If you look at the code, there are two files, strutil.h and strutil.cpp. They are very simple functions to handle char*. I found all the work you have to go through allocating memory for strcpy, etc.. annoying. So I replaced them with the funcs in those files. To prevent name collisions, I put them in the jkl_str namespace. I hope you find them useful.

    History

    5 Nov 2001 - updated download

    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
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionVS 2010 Pin
    norL25-Nov-12 0:09
    norL25-Nov-12 0:09 
    GeneralConnecting to sql server. Pin
    27-Oct-04 1:37
    suss27-Oct-04 1:37 
    GeneralesBig memory leak Pin
    Stober20-Aug-04 8:34
    Stober20-Aug-04 8:34 
    GeneralODBC_CONFIG_SYS_DSN problem.. pls help! Pin
    girl_lash11-Jul-04 21:49
    girl_lash11-Jul-04 21:49 
    GeneralODBC DriverMessage:ConnAttrFailed Pin
    G.V.Bhaskar28-Jan-02 19:43
    G.V.Bhaskar28-Jan-02 19:43 
    GeneralDemo available, finally Pin
    25-Oct-01 6:39
    suss25-Oct-01 6:39 
    GeneralHoller 2 Pin
    23-Oct-01 23:29
    suss23-Oct-01 23:29 
    GeneralHoller Pin
    16-Oct-01 1:52
    suss16-Oct-01 1:52 
    Generalam getting 9 errors, Pin
    9-Oct-01 2:16
    suss9-Oct-01 2:16 
    GeneralRe: am getting 9 errors, Pin
    rajendran_tt24-Oct-04 23:04
    rajendran_tt24-Oct-04 23:04 
    GeneralInvalid Heap! Pin
    29-Mar-01 7:10
    suss29-Mar-01 7:10 
    Generalexcellant Pin
    William Morris4-Sep-00 2:04
    William Morris4-Sep-00 2:04 

    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.