English

How To: Create a connection to an ArcSDE server with the ArcSDE C API

Summary

This sample C function retrieves ArcSDE connection parameters from environment variables and/or the command line. It also creates a connection to an ArcSDE server.

Procedure

This example first looks for specific environment variables, and uses their values for server name, instance name, database, user name and password.

Next, it parses the standard C command line (passed from the caller) for the options "-s" (server name), -i (instance name), "-D" (database name), "-u" (user name) and "-p" (password).

It then verifies that all necessary parameters are present. If so, it calls SE_connection_create.

To use this example from your C program, call this function using the argc and argv arrays received by main(). The function fills in the variable pointed to by the 'connection' parameter with an ArcSDE connection handle. The function returns an ArcSDE standard error code, or the error code defined in ERROR_MISSING_CONNECTION_PARAMETERS, if needed parameters are missing.


Code:
/*
***********************************
***
*** std_connection.c
*** Create an ArcSDE connection
*** using environment variables
*** and command line arguments
*** for connection parameters.
*** SAMPLE CODE
***
***********************************
*/

#include <stdlib.h>
#include <sdetype.h>
#include <sdeerno.h>
#include <string.h>
#define ENV_SERVER "SDESERVER"
#define ENV_INSTANCE "SDEINSTANCE"
#define ENV_DATABASE "SDEDATABASE"
#define ENV_USERNAME "SDEUSER"
#define ENV_PASSWORD "SDEPASSWORD"

/* Use a numeric code that you want to return */
/* if any needed parameters are missing */
#define ERROR_MISSING_CONNECTION_PARAMETERS -5501
LONG std_connection(int argc, char **argv,
SE_CONNECTION *connection, SE_ERROR *error)
{
LONG rc = SE_SUCCESS;

char *server=(char*)NULL;
char *instance=(char*)NULL;
char *database=(char*)NULL;
char *username=(char*)NULL;
char *password=(char*)NULL;
int i;

*connection = (SE_CONNECTION) NULL;

/* getvar will return a NULL if the environment variable does not exist */
server = getenv ( ENV_SERVER );
instance = getenv ( ENV_INSTANCE );
database = getenv ( ENV_DATABASE );
username = getenv ( ENV_USERNAME );
password = getenv ( ENV_PASSWORD );

for ( i = 0; i < (argc - 1); i++ )
{
if ( strcmp("-D", argv[i]) == 0 )
{
i++;
database = argv[i];
}
else if ( strcmp("-i", argv[i]) == 0 )
{
i++;
instance = argv[i];
}
else if ( strcmp("-p", argv[i]) == 0 )
{
i++;
password = argv[i];
}
else if ( strcmp("-s", argv[i]) == 0 )
{
i++;
server = argv[i];
}
else if ( strcmp("-u", argv[i]) == 0 )
{
i++;
username = argv[i];
}
}

if ((!server) ||
(!instance) ||
(!username) ||
(!password))
{
return ERROR_MISSING_CONNECTION_PARAMETERS;
}

rc = SE_connection_create( server, instance, database,
username, password,
error, connection);
return rc;
}