Switch to: V11V10V9V8V7V6V5

Lesson 2 - "Employees" Database on Server

In the previous lesson we have created an application for the work with local database. In the given lesson we will improve the application in such a way that it will be able to work with both local and remote databases located under Valentina Server. We will use the project got in the step 6 of the lesson 1 as the basis.

Step 1: Project Preparation for the Work with Server

Modification of initialization

Copy file Lesson1_step6 to the convenient place for you. Open it.

To solve the task of initialization modification you should create a new property gClient in App class and use it the function CreateOrOpenDatabase().

To do this you should do the next:

  • In the project window click App class.
  • Using “New Property” item of Edit menu create the property
mClient as Boolean

and

mConnection as VConnection

While doing this indicate the public access for it in the dialogue.

  • In App Source Code window indicates Events and choose Open().
  • Change the text in order it looks like this:.
mClient = true

if( mClient ) then
    Valentina.InitClient()
else
    Valentina.Init( 10 * 1024 * 1024 )
end if

VDatabase class object creation modification

For the work with Server we have to use another VDatabase creation form, that is why we have to modify the place of its creation.

To do this you should do the next:

  • Click Window1 in the project window.
  • Click Open() in the code editor window.
  • Change the text in order it looks like this:
dim f as FolderItem

f = GetFolderItem( “dbPersons.vdb” )

  if( app.mClient ) then
    mConnection = new VConnection( "localhost", "sa", "sa" )
    mConnection.Open()
    mDatabase = new VDatabase( mConnection )
  else
    mDatabase = new VDatabase()
  end 
  
  // If the Database exists we open it, otherwise we create it.
  try
    mDatabase.Open( f )
  catch err as VException
    if( err.ErrorNumber <> 532480 ) then // not a ConnectionError
      mDatabase.Create( f, EVDbMode.kDscDatBlbInd )
      CreateStructure()
    else
      MsgBox err.Message
      ' close current window
      Close()
      return
    end if
  end

If Valentina Server is located on another computer, you should change “localhost” for the address of your server.

Project start

In order to start this example you should have Valentina Server be already started. For better viewing of this example work we recommend you to start server in terminal window and to have VerboseLevel = 3 in ini-file of the server. Look in ValentinaServer.pdf more precisely.

After this project start, you may pay attention to the fact that database will be created not in its folder, but in the folder where Valentina Server stores database.

Step 2: Adding error checking

Improving Open() event

To do this you should do the next:

  • In App Source Code window indicate Events and choose Open().
  • Change the text in order it looks like this:.
dim f as FolderItem

f = GetFolderItem( “dbPersons.vdb” )

  if( app.mClient ) then
    try
      mConnection = new VConnection("localhost", "sa", "sa" )
      mConnection.Open()
      mDatabase = new VDatabase( mConnection )
    catch err as VException
      MsgBox err.Message
      ' close current window
      Close()
      return
     end try
  else
    mDatabase = new VDatabase()
  end 

  ...