Switch to: V9V8V7V6V5

Lesson 2 - Work with Server

In the previous lesson we have created an application that works with local database. In the given lesson we will change the application to work with remote database located under Valentina Server. We will use the project obtained in the lesson 1.

Prepare Application to Work with Server

To implement interaction with Valentina Server we just should change few things:

  1. Change the initializing of Valentina engine.
  2. Establish the connection to the server.
  3. Change the parameters to opening database.

Init Valentina and Create Connection

The VConnection class is used to work with Valentina. It provides some methods to create and close connections, create and open databases on server, execute SQL queries and so on.

So, a VConnection property should be added to the application.

 IBOutlet VCursorController* mpCursorController;

Now we should change the initialization of Valentina engine. And establish the connection with the server.

- (void)initValentina
{
	@try
	{
        [[Valentina instance] initClient];
 
        mpConnection = [[VConnection alloc] initWithHost:@"localhost" 
                                                userName:@"sa" 
                                                password:@"sa"];
    }
	@catch( ... ) 
	{
		NSRunCriticalAlertPanel( @"Valentina was not inited.",
					 @"", 
					 @"Ok", NULL, NULL );
	}
}

Open Database

To use database in the server we should initialize database using opened connection. To open the particular database it is enough to set it's name.

- (void)initDatabase
{
	NSString* databasePath = @"Employees.vdb";
 
        mpDatabase = [[VDatabase alloc] initWithConnection:mpConnection];
 
	@try
	{
		[mpDatabase open:databasePath];
	}
	@catch( ... )
	{
		@try
		{
			[mpDatabase create:databasePath];		
		}
		@catch ( ... ) 
		{
			NSRunCriticalAlertPanel( @"Database was not inited.",
                                                 @"", 
                                                 @"Ok", NULL, NULL );
		}
	}
}