## 27 Database connection ¶

An example which connects to and then disconnects from the database. provided you have already created the database in previous section

    #include <Foundation/Foundation.h>
    #include <EOAccess/EOAccess.h>
    #include <EOControl/EOControl.h>

    int
    main(int arcg, char *argv[], char **envp)
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      EOModelGroup *modelGroup = [EOModelGroup defaultGroup];
      EOModel *model = [modelGroup modelNamed:@"library"];
      EOAdaptor *adaptor;
      EOAdaptorContext *context;
      EOAdaptorChannel *channel;
      NSDictionary *dict = nil;

      /* Tools don't have resources so we have to add the model manually. */
      if (!model)
        {
          NSString *path = @"./library.eomodel";
          model = [[EOModel alloc] initWithContentsOfFile: path];
          dict = [model connectionDictionary];
          [modelGroup addModel:model];
          [model release];
        }

      adaptor = [EOAdaptor adaptorWithName:[model adaptorName]];
      [adaptor setConnectionDictionary: dict];
      context = [adaptor createAdaptorContext];
      channel = [context createAdaptorChannel];

      [channel openChannel];

      /* insert code here */

      [channel closeChannel];
      [pool release];
      return 0;
    }