Version: 1.0

Type: Function

Category: Databases

License: Other

Description: connection to interbase server with Borland 5.02 api



#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>
#include <stdio.h>
#include <ibase.h>



extern pr_error(long ISC_FAR *status, char ISC_FAR *operation);


typedef struct {
    char            userName[50];
    char            password[50];
    char            dbName[50];
    isc_db_handle   dbHandle;
    isc_tr_handle   transHandle;
    ISC_STATUS      status_vector[20];
    char            dpb_buffer[256], *dpb;
    short           dpb_length;
}InterParams, FAR * LPINTERPARAMS;




createDatabase(LPINTERPARAMS lpi)
{
    long            status[20];            /* status vector */
    long            sqlcode;               /* SQLCODE  */
    char            create_db[255];

    memset(create_db,0,sizeof(create_db));
    sprintf(create_db, "CREATE DATABASE '%s' USER '%s' PASSWORD '%s'", lpi->dbName,lpi->userName,lpi->password);



    if (isc_dsql_execute_immediate(lpi->status_vector, &lpi->dbHandle, &lpi->transHandle, 0, create_db, 1,
                                   NULL))
    {
        /* Extract SQLCODE from the status vector. */
        sqlcode = isc_sqlcode(lpi->status_vector);

        /* Print a descriptive message based on the SQLCODE. */
        if (sqlcode == -902)
        {
           MessageBox(GetFocus(),"Bu database zaten mevcut.",lpi->dbName,MB_ICONSTOP);
           return FALSE;
        }

        /* In addition, print a standard error message. */
        if (pr_error(status, "create database"))
            return FALSE;
    }

    isc_detach_database(status, &lpi->dbHandle);




   return TRUE;
}





connectDatabase(LPINTERPARAMS lpi)
{
 ISC_STATUS status_vector[20];
 char *p;

 lpi->dpb = lpi->dpb_buffer;
 *lpi->dpb++ = isc_dpb_version1;
 *lpi->dpb++ = isc_dpb_user_name;
 *lpi->dpb++ = strlen(lpi->userName);

    for (p = lpi->userName; *p;)
		*lpi->dpb++ = *p++;
		*lpi->dpb++ = isc_dpb_password;
		*lpi->dpb++ = strlen(lpi->password);
    for (p = lpi->password; *p;)
		*lpi->dpb++ = *p++;

     lpi->dpb_length = lpi->dpb - lpi->dpb_buffer;

     isc_attach_database(status_vector,0,lpi->dbName,&lpi->dbHandle,lpi->dpb_length,lpi->dpb_buffer);

		if (status_vector[0] == 1 && status_vector[1]){
			isc_print_status (status_vector);
			return FALSE;
		}


  return TRUE;
}






sendSQLCommand(LPINTERPARAMS lpi,LPSTR tableStr)
{

 ISC_STATUS status_vector[20];
 char ISC_FAR *create_tbl  = "CREATE TABLE deneme(adi varchar(30))";



   lstrcpy(create_tbl,tableStr);

    isc_start_transaction(status_vector, &lpi->transHandle, 1, &lpi->dbHandle,0,NULL);


    if (isc_dsql_execute_immediate(status_vector, &lpi->dbHandle, &lpi->transHandle, 0, create_tbl, 1, NULL))
        if (pr_error(status_vector, tableStr)){
            return FALSE;
        }

    isc_commit_transaction(status_vector, &lpi->transHandle);


   return TRUE;
}







createDenemeDatabase()
{
 InterParams   inter;
 int i;
 char ISC_FAR *create_tbl;


 char buf1[100];
 char buf[2000];


memset(buf,0,sizeof(buf));


    memset(&inter,0,sizeof(inter));


    inter.dbHandle=NULL;
    inter.transHandle=NULL;

    lstrcpy(inter.dbName,"C:\\WINDOWS\\DESKTOP\\DENEME2.GDB");
    lstrcpy(inter.userName,"SYSDBA");
    lstrcpy(inter.password,"masterkey");



     if(!createDatabase(&inter)){
       MessageBox(GetFocus(),"database cerate errorr","",MB_OK);
     }else {
       MessageBox(GetFocus(),"database cerate okay","",MB_OK);
     }


     if(!connectDatabase(&inter)){
       MessageBox(GetFocus(),"error connection","",MB_OK);
     }else {
       MessageBox(GetFocus(),"connection okay","",MB_OK);
     }





  return TRUE;
}