Re: WinXP & MSSQL & C & GTK+
- From: Kim Adil <ksadil bigpond net au>
- To: gtk-app-devel-list gnome org
- Subject: Re: WinXP & MSSQL & C & GTK+
- Date: Wed, 07 Sep 2005 20:16:59 +1000
Alf,
Here is the code of includes, and procedures that I use in my gtk
application compiled on linux, xp,98 and 2000. Works well 24/7 for 2
years. code is ugly but working. Database is irrelevant, I have used it
with access, sqlserver, postgresql, and mysql sucessfully. This code
runs in production with a sqlserver database. I use dev-cpp in windows
and anjuta in linux.
I do have some code in another program that interrogates failed calls
and returns debugging info so you can fed it back through a dialog to
the user. I can dig it up and send it through next week.
You might find variable names to be suspiciously similar to various
tutorial examples around the net. Cut and paste programming I'm afraid.
I included the variable "button" in the procedure call so I could use a
gtk_timeout to periodically run the query.
Regards,
Kim
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "stdio.h"
#include "windows.h"
#include "stdio.h"
#include "sql.h"
#include "SQLEXT.H"
#include <string.h>
#include <stdlib.h>
.
.
.
int odbccall1(GtkWidget *button)
{
HENV henv = NULL;
HDBC hdbc1 = NULL;
HSTMT hstmt1 = NULL;
UCHAR szDSN[SQL_MAX_DSN_LENGTH] =
"mydsn"; // Data
Source Name buffer
UCHAR szUID[10] = "MyUsername"; // User ID buffer
UCHAR szPasswd[10] = "MyPassword"; // Password
buffer
UCHAR szSqlStr[] = "SELECT truck, loader FROM RowQuery";
UCHAR szTruck[8]; // Machine name buffer
SDWORD cbTruck;
UCHAR szLoader[20]; // Model buffer bytes
recieved
SDWORD cbLoader;
UCHAR szMachNm[8]; // Machine name buffer
SDWORD cbMachNm;
UCHAR szActId[20]; // Model buffer bytes
recieved
SDWORD cbActId;
UCHAR szModelDs[30]; // Activity buffer
SDWORD cbModelDs;
UCHAR szOpNm[30]; // CircuitNo
SDWORD cbOpNm;
UCHAR szActivity[50]; // Model buffer
SDWORD cbActivity;
UCHAR szGpsZone[50]; // Model buffer
SDWORD cbGpsZone;
UCHAR szBCM[10]; // Model buffer
SDWORD cbBCM;
UCHAR szSG[10]; // Model buffer
SDWORD cbSG;
UCHAR szShCycles[30]; // Model buffer
SDWORD cbShCycles;
UCHAR szassign_locn_1[10]; // Model buffer
SDWORD cbassign_locn_1;
UCHAR szassign_locn_2[10]; // Model buffer
SDWORD cbassign_locn_2;
UCHAR szis_to_loader[10]; // Model buffer
SDWORD cbis_to_loader;
UCHAR szhaulpath_perct[10]; // Model buffer
SDWORD cbhaulpath_perct;
UCHAR szis_on_circuit[10]; // Model buffer
SDWORD cbis_on_circuit;
UCHAR szloader_loading[10]; // Model buffer
SDWORD cbloader_loading;
UCHAR sztelem_status[10]; // Model buffer
SDWORD cbtelem_status;
double tmp=0;
gchar message[100];
int i,k,j,trucks;
RETCODE retcode;
GtkWidget *w;
w=lookup_widget(button,"label1");
if(strcmp(odbccallstatus,"active")==0)
{
printf("odbc call alreamdy in progress");
return(1);
}
sprintf(odbccallstatus,"active");
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv); //
Allocate
the ODBC Environment and save handle.
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
// fprintf(debugLog,"odbc call failed 1 to allocate
handle");
sprintf(odbccallstatus,"inactive");
return(2);
}
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)
SQL_OV_ODBC3, SQL_IS_INTEGER); // Let ODBC know this is an ODBC 3.0
application.
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
// fprintf(debugLog,"odbc call failed 2 to set version");
sprintf(odbccallstatus,"inactive");
return(3);
}
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1); //
Allocate
an ODBC connection and connect.
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
// fprintf(debugLog,"odbc call failed 3 to allocate a
connection");
sprintf(odbccallstatus,"inactive");
return(4);
}
retcode = SQLConnect (hdbc1, szDSN, SQL_NTS, szUID, SQL_NTS,
szPasswd,
SQL_NTS);// Connect to the data source "oracle" using userid and
password.
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
// fprintf(debugLog,"odbc call failed 4 to connect");
sprintf(odbccallstatus,"inactive");
return(5);
}
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1); //
Prepare and execute an SQL statement on the statement handle.Uses a
default result set because no cursor attributes are set.
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
sprintf(odbccallstatus,"inactive");
return(6);
}
retcode = SQLPrepare(hstmt1,"{call
dbo.uspGetPitMonitorData(3)}",
SQL_NTS);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
sprintf(odbccallstatus,"inactive");
gtk_label_set_text(w,"stored procedure failed to
execute!");
return(7);
}
retcode = SQLExecute(hstmt1); // Simplified result
set
processing. Bind one column and
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
sprintf(odbccallstatus,"inactive");
return(8);
}
retcode = SQLBindCol (hstmt1, 2, SQL_C_CHAR, szMachNm,
sizeof(szMachNm), &cbMachNm);// Project only column 1 which is the
models
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 3, SQL_C_CHAR, szActId,
sizeof(szActId),
&cbActId);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 5, SQL_C_CHAR, szModelDs,
sizeof(szModelDs), &cbModelDs);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 6, SQL_C_CHAR, szOpNm,
sizeof(szOpNm),
&cbOpNm);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 4, SQL_C_CHAR, szActivity,
sizeof(szActivity), &cbActivity);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 7, SQL_C_CHAR, szGpsZone,
sizeof(szGpsZone), &cbGpsZone);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 8, SQL_C_CHAR, szBCM,
sizeof(szBCM),
&cbBCM);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 9, SQL_C_CHAR, szSG, sizeof(szSG),
&cbSG);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 10, SQL_C_CHAR, szShCycles,
sizeof(szShCycles), &cbShCycles);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 11, SQL_C_CHAR, szassign_locn_1,
sizeof(szassign_locn_1), &cbassign_locn_1);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 12, SQL_C_CHAR, szassign_locn_2,
sizeof(szassign_locn_2), &cbassign_locn_2);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 13, SQL_C_CHAR, szis_to_loader,
sizeof(szis_to_loader), &cbis_to_loader);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 14, SQL_C_CHAR, szhaulpath_perct,
sizeof(szhaulpath_perct), &cbhaulpath_perct);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 15, SQL_C_CHAR, szis_on_circuit,
sizeof(szis_on_circuit), &cbis_on_circuit);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 18, SQL_C_CHAR, sztelem_status,
sizeof(sztelem_status), &cbtelem_status);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
retcode = SQLBindCol (hstmt1, 19, SQL_C_CHAR, szloader_loading,
sizeof(szloader_loading), &cbloader_loading);
if((retcode != SQL_SUCCESS)&&( retcode !=
SQL_SUCCESS_WITH_INFO))
{
//gtk_label_set_text(w,"odbc call failed");
sprintf(odbccallstatus,"inactive");
return(9);
}
i=0;
TotNumMachines=0;
//gtk_label_set_text(w,"got to here");
while ( ( retcode = SQLFetch(hstmt1) ) != SQL_NO_DATA )
{
sprintf(ODBCmachineArray[i].machineName,szMachNm);
sprintf(ODBCmachineArray[i].model,szModelDs);
sprintf(ODBCmachineArray[i].act_id,szActId);
ODBCmachineArray[i].haulpath_perct=atoi(szhaulpath_perct);
ODBCmachineArray[i].is_on_circuit=atoi(szis_on_circuit);
sprintf(ODBCmachineArray[i].operator,szOpNm);
sprintf(ODBCmachineArray[i].activity,szActivity);
sprintf(ODBCmachineArray[i].gpsZone,szGpsZone);
sprintf(ODBCmachineArray[i].assign_locn_1,szassign_locn_1);
sprintf(ODBCmachineArray[i].assign_locn_2,szassign_locn_2);
sprintf(ODBCmachineArray[i].is_to_loader,szis_to_loader);
sprintf(ODBCmachineArray[i].loader_loading,szloader_loading);
ODBCmachineArray[i].shift_cycles=atoi(szShCycles);
if(ODBCmachineArray[i].machineName[0]=='D'){
tmp=(atof(szBCM))/atof(szSG);
sprintf(ODBCmachineArray[i].BCM,"%8.0f
BCM",tmp);
}
ODBCmachineArray[i].alarmLevel=0;
if(((strcmp("0",ODBCmachineArray[i].act_id)==0)||(strcmp("1",ODBCmachineArray[i].act_id)==0))||((strcmp("17",ODBCmachineArray[i].act_id)==0)||(strcmp("32",ODBCmachineArray[i].act_id)==0)))
{
ODBCmachineArray[i].alarmLevel=1;
}
if(((strcmp("1135",ODBCmachineArray[i].act_id)==0)||(strcmp("1136",ODBCmachineArray[i].act_id)==0))||(strcmp("1224",ODBCmachineArray[i].act_id)==0))
{
ODBCmachineArray[i].alarmLevel=2;
}
if(strcmp(sztelem_status,"OK")!=0){
ODBCmachineArray[i].alarmLevel=9;
}
i++;
TotNumMachines++;
numberOfODBCMachines=TotNumMachines;
}
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
sprintf(odbccallstatus,"inactive");
return(0);
}
On Tue, 2005-09-06 at 18:36 +0200, Alf Stockton wrote:
Can someone please give me, preferably source, but at least hints on how
to call MSSQL Stored Procedurers via ODBC from a C program written using
Glade & GTK+.
All this is, of course, being developed under Windows.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]