Connect to the site database

Use the SmsDataSourceConnect function to connect to the site database that you want to access.

Currently, SMS supports one database type: Microsoft SQL Server. However, the SMS API is designed to allow support of other types of databases in the future.

The SmsDataSourceConnect function takes two parameters:

Note that your application can connect to one site database at a time.

Example:

// Function to make a datasource connection to
// a site database. This function prompts for
// SQL server, loginID, password, and database,
// and then calls SmsDataSourceConnect to get 
// a connection handle.

SMS_STATUS ConnectToSiteDatabase (HANDLE *phConnect)
                              // Pointer to handle to receive
                              // connection handle.
{

SMS_STATUS stat;

//Structure that specifies the site database to connect to.
DATASOURCE dsParams;
// Create string buffers for connecting to the database.
char szServer[33];
char szDb[31]; //maximum length of SQL Server database name is 30.
char szUser[31]; //maximum length of SQL Server Login ID is 30.
char szPasswd[31];

// Get database information from user and establish connection.
printf("Connect to site database.\n");

printf("SQL Server: ");
gets( szServer );

printf("Database: ");
gets( szDb );

printf("SQL Server Login ID: ");
gets( szUser );

printf("Password: ");
gets( szPasswd );

// Assign connection information to dsParams structure.
// The type of database.
dsParams.sqlParams.ds          = DB_SQL;
// SQL Server name.
dsParams.sqlParams.pszServer   = szServer;
// Login ID.
dsParams.sqlParams.pszUserName = szUser;
// Password for Login ID.
dsParams.sqlParams.pszPasswd   = szPasswd;
// Name of the site database.
dsParams.sqlParams.pszDbName   = szDb;
// Pointer to encryption function.
// No function is used in this example.
dsParams.sqlParams.pFunc       = NULL;
//Encryption key. No encryption key is used in this example.
dsParams.sqlParams.pszKey      = "";

// Use SmsDataSourceConnect to make a connection to the site database
// and assign the connection handle to hConnect.
stat = SmsDataSourceConnect( &dsParams, phConnect );
    
if (stat == SMS_OK) {
    printf("Connected to database %s on SQL Server %s as %s.\n",
            szDb, szServer, szUser);
}
else {
    printf("Cannot connect to SQL Server. Error code: %d\n", stat);
}

return stat;

}