Featured Database Articles

ODBC DSN-Less Connection Tutorial - Page 2

Show Me The Code

Other than the standard "house cleaning" code, there are really three important functions. The first is
the code which parses the ini file and extracts the login parameters. Once located, each parameter is
stored in a global variable and another variable is populated with the complete connect string.

The next important piece of code is the function which validates the parameters passed. This is done
by turning OFF the error handling and trying to use our newly created connection string to do some work.
If an error results, we know the connection string has a problem. In this example, we are trying to
delete an Author that doesn't exist from the Pubs database. Even though no records are deleted, the
query will not return an error unless the connection fails. (If you are uncomfortable with the idea
of executing a DELETE Query, then use a SELECT statement instead. Personally, I like to live on the edge.)

Conclusion

I've left out much of the code that deals with variable declaration and database objects, but it's all
available in the download. Remember that this tutorial was created as a tool to help me explain DSN-Less
ODBC to people who didn't know where to start. My login form is not intended to be a "cut-and-paste"
enterprise solution, but rather, an idea that was born to be enhanced. In fact, we have one login form
that reads and writes the connection parameters to and from the Windows Registry, saving the user's
conneciton information and allowing different users to connect the same client to different back-end
databases. No doubt, you already have ideas of how this code can be adapted to better fit your program.