
January 2nd, 2004, 11:33 AM
|
|
Junior Member
|
|
Join Date: Nov 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
MS Access to SQL Server, using C++ and ODBC
I'm doing a project and have hit a brick wall. We started off using Access for dev and testing and now we want to move to a real DB, MS SQL Server. I imported the DB using the MS SQL Server import/export tool, changed the connect string in the code, but can't seem to access the data from my code. I can see the tables using Query Analyzer tool.
Here's the C++ code. The SQLPrepare statement succeeds but the SQLExecute statement fails.
Code:
void ADB::OpenIt()
{
UCHAR szUID[10] = "Admin";// User ID buffer
UCHAR szPasswd[10] = "";// Password buffer
RETCODE rc;
int iOut;
char strOut[256];
char szDSN[256] = DBConn;
char lpszDSN[256] = "VV2";
char lpszUid[256] = "user1";
char lpszPwd[256] = "userpass";
m_hEnv = NULL; // Env Handle from SQLAllocEnv()
m_hDBC = NULL; // Connection handle
SQLAllocEnv (&m_hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (m_hEnv, &m_hDBC);
rc = SQLConnect(m_hDBC, (SQLCHAR*) lpszDSN, SQL_NTS, (SQLCHAR*) lpszUid, SQL_NTS, (SQLCHAR*) lpszPwd, SQL_NTS );
if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
DisplayIt( "ADB:Openit db connection OK" );
else
DisplayIt( "ADB:Openit db connection failed" );
}
void ADB::CloseIt()
{
SQLDisconnect (m_hDBC);
SQLFreeConnect (m_hDBC);
SQLFreeEnv (m_hEnv);
}
void ADB::GetIt()
{
HSTMT hStmt = NULL;// Statement handle
RETCODE rc;
SQLINTEGER lenrcvd;// buffer bytes recieved
UCHAR szSqlStr[256];
SQLINTEGER idate;
SQLINTEGER itime;
SQLCHAR userid[51];
SQLCHAR password[51];
int j;
char emsg[100];
DisplayIt( "ADB:GetIt entered OK" );
rc = SQLAllocStmt (m_hDBC, &hStmt);
sprintf (emsg, "SQLAllocStmt rc is %d", rc);
DisplayIt(emsg); // this rc comes back 0
strcpy(szSqlStr, "SELECT * from VVAppDB.dbo.Passwords");
rc = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
sprintf (emsg, "SQLPrepare rc is %d", rc);
DisplayIt(emsg); // this rc comes back 0
rc = SQLExecute (hStmt);
sprintf (emsg, "SQLExecute rc is %d", rc);
DisplayIt(emsg); // this rc comes back -1
SQLBindCol (hStmt, 1, SQL_C_CHAR, &userid, sizeof(userid), &lenrcvd);
SQLBindCol (hStmt, 2, SQL_C_CHAR, &password, sizeof(password), &lenrcvd);
j = 0;
while ((rc = SQLFetch(hStmt)) != SQL_NO_DATA)
{
strcpy(uid[j], userid);
strcpy(pw[j], password);
j++;
}
usercnt = j;
SQLFreeStmt (hStmt, SQL_DROP);
}
Any suggestions?
|