12-02-2021 04:41 PM
I am at a loss. I have 2 identical databases. One is an Access database and the other is on an SQL server. I am able to read all of the data just fine except for columns configured as a "bit". Under Access, the record shows as a "1". Using the exact same code and accessing the SQL database the value comes back as "-1". The value should be 1 because that is what the data states when I look in the table itself. Records with a data value of "0" read the same from both databases. I have tried both mapping and binding the column using int, char and bool and all report the same -1. Below is some of the code. Thanks for helping!!
// SQL program to read a record in a database
#include "cvi_db.h"
#include <ansi_c.h>
#include <userint.h>
#include <utility.h>
void ShowError()
{
MessagePopup("Database Error",DBErrorMessage());
}
void main()
{
int hDBConnection = 0; // Handle to database connection
int hMapDef = 0; // Handle to map definition
int hSQLStatment = 0; // Handle to SQL statement
int iResultCode; // Result code
char sOperator[30] = ""; // Operator name
char sLogin[30] = ""; // Operator's login ID
int iOperatorKey; // Operator table key
char sAuthorized[30] = ""; // Person authorized (1 = Yes, 0 = No)
int iValidOperator; // Operator trained (1 = Yes, 0 = No)
int iLocation; // Location key for where operator is located
int iOperatorKeyStat; // Fetch status for
int iOperatorStat; // Fetch status for
int iAuthorizedStat; // Fetch status for
int iValidStat; // Fetch status for
int iLoginIDStat; // Fetch status for
int iLocationKeyStat; // Fetch status for
SetStdioWindowVisibility (1);
// Connect to database
hDBConnection = DBConnect ("DSN=CVI SQL Samples");
//hDBConnection = DBConnect ("DSN=MFGDatabase; UID=Loan_usr; PWD=***");
if (hDBConnection <= 0)
{
// Some error occured
{ShowError(); goto Error;}
}
// Map for constructed SQL statement
//hMapDef = DBBeginMap (hDBConnection);
hMapDef = DBActivateSQL(hDBConnection, "SELECT Operator_Key, Operator, Authorized, Valid, Login_ID, Location_Key FROM Operator");
if (hMapDef <= 0)
{
// Some error occured
{ShowError(); goto Error;}
}
iResultCode = DBBindColInt (hMapDef, 1, &iOperatorKey, &iOperatorKeyStat);
iResultCode = DBBindColChar (hMapDef, 2, 30, sOperator, &iOperatorStat, "");
iResultCode = DBBindColChar (hMapDef, 3, 30, sAuthorized, &iAuthorizedStat, "");
iResultCode = DBBindColInt (hMapDef, 4, &iValidOperator, &iValidStat);
iResultCode = DBBindColChar (hMapDef, 5, 30, sLogin, &iLoginIDStat, "");
iResultCode = DBBindColInt (hMapDef, 6, &iLocation, &iLocationKeyStat);
// Fetch the values for each column of the record
while ((iResultCode = DBFetchNext (hMapDef)) == DB_SUCCESS)
{
printf("Operator Key: %i, Login ID: %s, Operator: %s, Authorized: %s, Valid: %i, Location: %i \n",
iOperatorKey, sLogin, sOperator, sAuthorized, iValidOperator, iLocation);
}
if ((iResultCode != DB_SUCCESS) && (iResultCode != DB_EOF))
{ShowError(); goto Error;}
iResultCode = DBDeactivateMap (hMapDef);
if (iResultCode != DB_SUCCESS) {ShowError(); goto Error;}
iResultCode = DBDisconnect (hDBConnection);
if (iResultCode != DB_SUCCESS) {ShowError(); goto Error;}
return;
Error:
return;
}
12-03-2021 05:18 AM
While the value may be shown as 1 when you look at it through the SQL Server Browser, that is not what your SQL Connector wants to return to you, and strictly speaking it is not even wrong. A bit value is simply a boolean and there are many possible definitions for that. As long as it is only a real bit it is simple, but usually it is encoded as a number and a very common definition of a boolean is FALSE is 0 and everything else is TRUE.
Now there are about a dozen different ways to connect to SQL Server. You can use an ODBC driver, ADO/DAO, or even a .Net connector and you can use one from Microsoft or a number of others. Depending on what driver you use there are potentially several layers of translation that your data goes through. Eg. the CVI Toolkit goes through the ADO interface, and if you have a .Net driver it will go also through the ADO-.Net bridge. Each of these layers does its own transformations and optimizations.
So the solution is:
1) you can try to find a driver that will result in your expected outcome
2) you can write your software to treat the bit column according to the rule 0 = FALSE, anything else = TRUE
12-07-2021 05:24 PM
Bummer, I was hoping I was just doing something stupid. I guess I will just have to either live with it or find a work-around.
Appreciate your feedback!