Respected sir,
We are doing one database for the exam dept. As we want to retrieve data as name,result,total, by giving rollno as primary key.
The problem with this is that we can not retrieve data by entering the rollno of seven digits.So can you help in correcting the code?
Thanks for your
interest.Jbhusane@yahoo.com
We have tried to make such database as:
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
#include <mbstring.h>
#define max 10
int main(void)
{
UCHAR szstr[10];
printf("Enter number");
scanf("%s",&szstr);
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL; // Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "Result"; // Data Source Name buffer
UCHAR* szUID = NULL; // User ID buffer
UCHAR* szPasswd = NULL; // Password buffer
typedef struct
{
SQLCHAR name[25];
SQLINTEGER cbname;
SQLCHAR StudentID;
SQLINTEGER cbstudentid;
SQLCHAR result[20];
SQLINTEGER cbresult;
SQLCHAR total[5];
SQLINTEGER cbtotal;
SQLCHAR Grade;
SQLINTEGER cbgrade;
}Student;
Student szStudent[max];
UCHAR szSqlStr[]= "Select name,result,grade From Total_Student Where rollno ="; // SQL string
_mbscat(szSqlStr,szstr);
RETCODE retcode; // Return code
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "db97" 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)
{
printf("Success\n");
// 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);
// Project only column 1 which is the models
SQLBindCol (hStmt, 1, SQL_C_CHAR, szStudent[0].name, sizeof(szStudent[0].name), &szStudent[0].cbname);
SQLBindCol (hStmt, 2, SQL_C_CHAR, szStudent[0].result, sizeof(szStudent[0].result), &szStudent[0].cbresult);
SQLBindCol (hStmt, 2, SQL_C_CHAR, szStudent[0].result, sizeof(szStudent[0].result), &szStudent[0].cbresult);
SQLBindCol (hStmt, 3, SQL_C_CHAR, szStudent[0].total, sizeof(szStudent[0].total), &szStudent[0].cbtotal);
// Get row of data from the result set defined above in the statement
retcode = SQLFetch (hStmt);
while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
if (szStudent[0].cbname == SQL_NULL_DATA)
printf(" NULL ");
else
printf("%s\t", szStudent[0].name);
if (szStudent[0].cbresult == SQL_NULL_DATA)
printf(" NULL ");
else
printf("%s\t", szStudent[0].result);
if (szStudent[0].cbtotal == SQL_NULL_DATA)
printf(" NULL ");
else
printf("%s\t", szStudent[0].total);
retcode = SQLFetch (hStmt); // Fetch next row from result set
}
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect (hDBC);
}
else
{
printf("Not entered in database\n");
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
return 0;
}