SQLExecute failing, Need Some Pointers
I am working with MS VS C++ 6.0 building a dialog based app to delete records from SQL Server 2000 db. I am connecting to the db and table fine, but the SQLExecute command is failing. I have tried to write some error return code to catch the error but have had poor results, as I am new to SQL server based stuff.
I am connecting to the db as an admin so and I have written similar code that works to do select statements and bind columns, but the delete fails. I attached some code below. Any help is greatly appreciated.
Code:
//get db info to see if tote exists in inventory
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL;// Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "purge";// Data Source Name buffer
UCHAR szUID[10] = "##";// User ID buffer
UCHAR szPasswd[10] = "##";// Password buffer
UCHAR szSqlStr [200];
char AdjustLog [ ] = "DELETE FROM HEBMeatIntrack..AdjustLog WHERE (TransLogID like '";
char endquote [] = "%');"; //syntax to end sql statement
string statement;
string table = "HEBMeatInTrack..AdjustLog";
RETCODE retcode;
//display table to edit box for user visual of the table being purged
// strcpy (m_Table, table.c_str());
//set statement
statement += AdjustLog;
statement += translogid;
statement += endquote;
strcpy ((char*) szSqlStr, statement.c_str());
//testing purposes only
ofstream fout;
fout.open("D:\\HEB\\MeatPlant\\Purge\\purgelog.txt", ios::app);
fout <<"Inside DeleteAdjustLog Function"<<endl;
fout <<"SQL Statement: "<<statement.c_str()<<endl;
fout <<"TranslogID : "<<translogid<<endl;
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "test" using userid and password.
retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
// Prepare the SQL statement by assigning it to the statement handle
retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
fout<<"SQLExecute Successful"<<endl;
}
else if (retcode == SQL_ERROR || retcode == SQL_NO_DATA || retcode==SQL_INVALID_HANDLE)
{
displayODBCError (retcode);
}
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect (hDBC);
fout.close();
}
else
{
AfxMessageBox ("ERROR: Could Not Connect to Database: HEBMeatInTrack!\nPlease Contact I.S. Support.");
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);