#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
HENV henv;
HDBC hdbc;
HSTMT hstmt;
int ConnecToDB ( unsigned char *id, unsigned char *pwd, unsigned
char *dsn )
{
RETCODE retcode;
retcode = SQLAllocEnv(&henv);
if(retcode == SQL_SUCCESS) {
retcode = SQLAllocConnect(henv,&hdbc);
if(retcode == SQL_SUCCESS)
{
SQLSetConnectOption(hdbc,SQL_LOGIN_TIMEOUT,5);
retcode =
SQLConnect(hdbc,dsn,SQL_NTS,id,SQL_NTS,pwd,SQL_NTS);
if(retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO)
{
return 1;
}
return 0;
}
return 0;
}
return 0;
}
int DisconnectDB()
{
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return 1;
}
void insert_rec(void)
{
char name[20],addr[40],tel[20],mobile[20],email[20];
char query[1048];
RETCODE retcode;
printf("\n>>>사용자로부터 레코드를 입력받아
데이터베이스에 저장\n\n");
printf("Enter name :");
gets(name);
printf("Enter address :");
gets(addr);
printf("Enter telephone number :");
gets(tel);
printf("Enter telephone mobile :");
gets(mobile);
printf("Enter e-amil :");
gets(email);
printf("\n입력 레코드 : %s %s %s %s
%s\n\n",name,addr,tel,mobile,email);
if(ConnecToDB("","","mydsn" ) == 0)
{
printf("db접속 에러");
return;
}
sprintf(query,"insert into address values
('%s','%s','%s','%s','%s')",name,addr,tel,mobile,email);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
if( retcode == SQL_SUCCESS ) printf("입력 성공\n\n");
else printf("입력에 실패 하였습니다.\n\n");
DisconnectDB();
}
void search_all_rec(void)
{
char query[1048];
RETCODE retcode;
SQLCHAR szA[1048];
SQLINTEGER cbA ;
int nColCnt;
printf("\n>>>전체 레코드를 출력\n\n");
if(ConnecToDB("","","mydsn" ) == 0)
{
printf("db접속 에러");
return;
}
strcpy(query,"select * from address");
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,(unsigned char *)query,
SQL_NTS);
if( retcode != SQL_SUCCESS)
{
printf("질의에 오류가 있습니다.\n\n");
return;
}
while(!SQLFetch(hstmt) )
{
nColCnt = 1;
while(1)
{
retcode =
SQLGetData(hstmt,nColCnt,SQL_CHAR,&szA,1048,&cbA);
if(retcode == -1) break;
printf("%s\t",szA);
nColCnt++;
}
printf("\n");
}
DisconnectDB();
}
search_by_title(void)
{
char name[20];
char query[1048];
RETCODE retcode;
SQLCHAR szA[1048];
SQLINTEGER cbA ;
int nColCnt;
printf("\n>>>사용자로부터 입력받은 검색어를 통해
만족하는 레코드 출력\n\n");
printf("Name : ");
gets(name);
if(ConnecToDB("","","mydsn" ) == 0)
{
printf("db접속 에러");
return;
}
strcpy(query,"select * from address where
name='%s'",name);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,(unsigned char *)query,
SQL_NTS);
if( retcode != SQL_SUCCESS)
{
printf("질의에 오류가 있습니다.\n\n");
return;
}
while(!SQLFetch(hstmt) )
{
nColCnt = 1;
while(1)
{
retcode =
SQLGetData(hstmt,nColCnt,SQL_CHAR,&szA,1048,&cbA);
if(retcode == -1) break;
printf("%s\t",szA);
nColCnt++;
}
printf("\n");
}
DisconnectDB();
}
void main(void)
{
char ch;
do{
printf("***************************************\n");
printf("* 주 소 록*\n");
printf("*****************************************\n");
printf("해당메뉴를 입력하고 리턴을 치시오\n");
printf("Command : (1) Insert (2) Search_ALL (3) Search_by_Title
(4) Quit > ");
ch = getchar();
fflush(stdin);
switch(ch) {
case '1' : insert_rec();
break;
case '2' : search_all_rec();
break;
case '3' : search_by_title();
break;
case '4' : break;
default : printf("메뉴가 없습니다.\n");
}
}while(ch != '4');
}
이름으로 검색하는 함수에서 검색은 되는거 같은데 결과가 화면에
출력이 되지 않는데 어디서 잘못된건가요?
|