If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Regarding ODBC in SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-22-04, 11:12
Jayesh2011905 Jayesh2011905 is offline
Registered User
 
Join Date: Mar 2004
Location: India
Posts: 2
Regarding ODBC in SQL

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;
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On