The following procedure creates a tutorial program that establishes a connection with the UltraLite CustDB sample database and executes a query.
Start Microsoft eMbedded Visual C++.
From the Start menu, choose All Programs > Microsoft eMbedded Visual C++ 4.0 > eMbedded Visual C++ 4.0
In eMbedded Visual C++ create a new workspace named Ultralite:
Select File > New.
Click the Workspaces tab.
Choose Blank Workspace. Specify a workspace name Ultralite and specify C:\tutorial as the location to save this workspace. Click OK.
The Ultralite workspace is added to the Workspace window.
Create a new project named esql and add it to the Ultralite workspace.
Select File > New.
Click the Projects tab.
The application types available depend on the SDKs you have installed. Choose an application type appropriate for the device you are using. For example, if you are using a Pocket PC 2002 device, choose WCE Pocket PC 2002 Application. Specify a project name esql and select Add To Current Workspace. Select the applicable CPUs. Click OK.
Choose Create An Empty Project and click Finish.
The project is saved in the c:\tutorial\esql folder.
Create the sample.sqc source file.
Choose File > New.
Click the Files tab.
Select C++ Source File.
Select Add to Project and choose esql from the dropdown list.
Name the file sample.sqc. Click OK.
Copy the following source code into the file. The next section describes this tutorial code in detail.
#include <tchar.h> #include <windows.h> EXEC SQL INCLUDE SQLCA; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd) { /* Declare fields */ EXEC SQL BEGIN DECLARE SECTION; long pid=1; long cost; TCHAR pname[31]; EXEC SQL END DECLARE SECTION; TCHAR output[200]; TCHAR result[10]; /* Before working with data*/ db_init(&sqlca); /* Connect to database */ EXEC SQL WHENEVER SQLERROR GOTO error; EXEC SQL CONNECT USING 'dbf=\Windows\Start Menu\esqldb.udb'; /* Fill table with data first */ EXEC SQL INSERT INTO ULProduct( prod_id, price, prod_name) VALUES (1, 400, '4x8 Drywall x100'); EXEC SQL INSERT INTO ULProduct ( prod_id, price, prod_name) VALUES (2, 3000, '8''2x4 Studs x1000'); /* Commit changes (INSERTs) to database */ EXEC SQL COMMIT; /* Fetch row 1 from database */ EXEC SQL SELECT price, prod_name INTO :cost, :pname FROM ULProduct WHERE prod_id= :pid; /* pid was initialized to 1, so get first row inserted above */ /* Print query results */ wsprintf( output , TEXT("Product id: %d\n price: %d\n name: %s") , pid , cost , pname ); MessageBox(NULL, output, result, MB_OK); /* Preparing to exit: disconnect */ EXEC SQL DISCONNECT; db_fini(&sqlca); return(0); /* Error handling. If the row does not exist, or if an error occurs, -1 is returned */ error: if((SQLCODE==SQLE_NOTFOUND)||(SQLCODE<0)) { wsprintf (output, TEXT("error: %d"), SQLCODE ); MessageBox(NULL, output, result, MB_OK); return(-1); } }
Save the file.
Configure the sample.sqc source file settings to invoke the SQL preprocessor to preprocess the source file:
Right-click sample.sqc in the Workspace window and choose Settings.
The Project Settings dialog appears.
From the Settings For dropdown list, choose All Configurations.
In the Custom Build tab, enter the following statement in the Commands field. Ensure that the statement is entered all on one line.
"%SQLANY10%\win32\sqlpp.exe" -q -u $(InputPath) sample.cpp
This statement runs the SQL preprocessor sqlpp on the sample.sqc file, and writes the processed output to a file named sample.cpp. The SQL preprocessor translates SQL statements in the source file into C/C++ function calls.
For more information about the SQL preprocessor, see SQL preprocessor.
Type sample.cpp in the Outputs field.
Click OK to submit the changes.
Preprocess the sample.sqc file.
Select sample.sqc in the Workspace window. Choose Build > Compile sample.sqc. A sample.cpp file is created and saved in the tutorial\esql directory.
Add sample.cpp (the output of the SQL preprocessor) to the project:
Right-click the Source Files folder in the Workspace window and choose Add Files to Folder.
Browse to c:\tutorial\esql\sample.cpp and click OK.
The sample.cpp file now appears inside the Source Files folder.