C语言如何连接数据库?

 C语言如何连接数据库?

附一源码

CWinApp theApp;
using namespace std;

CHmbAdoDBRs RfRst;
CString sSql;
CString seq, body, eng, tm, tl, icol, ecol, alc, bhmcdate;
int i;

BOOL DbOpen();
BOOL CkseqtmpSelect();
BOOL CkseqtmpUpdate();
int PrintStatus();
int SockInit();

CHmbAdoDB Sqldb;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int cnt = 0;
int nRetCode = 0;
int page = 1;
int prt_ret;
CString strDate, strTime;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}

//DataBase Open...
if(DbOpen()) cout << "DataBase Open Open Ok" << endl;
else {
cout << "DataBase Open Error/n" << endl;
return -1;
}

while( 1 ) {
ret = SockInit(); //建立socket连接
if ( ret < 0 ) Sleep(3000);
else break;
}

while( 1 ) {
if ( CkseqtmpSelect() == FALSE ) { // CKSEQTMP FLAG03='0' SELECT
Sleep(3000);
continue;
}

while( 1 ) { // PRINTER STATUS Check
prt_ret = PrintStatus();
if ( prt_ret == 1 ) {
closesocket(sid);
WSACleanup();

while( 1 ) {
ret = SockInit();
if ( ret < 0 ) Sleep(3000);
else break;
}
break;
}
else {
printf("Printer Status Check !!!/n");
Sleep(3000);
}
}

printf("%s-%s-%s-%s-%s-%s-%s-%s-%s/n", seq, body, eng, tm, tl, icol, ecol, alc, bhmcdate);
memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, "%c%c1", ESC, W);
ret = send( sid, sbuf, strlen(sbuf), 0 );

if ( cnt == 0 || atoi(seq) < 2 ) {
if ( atoi(seq) < 2 ) {
cnt = 0;
page = 1;
memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, "%c%c%c", FF, CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );
}

memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, " [ BUMPER ASS'Y WORK ORDER ]%c%c%c%c%c%c", CR, LF, CR, LF, CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );

strDate.Format("%s-%s-%s", bhmcdate.Left(4), bhmcdate.Mid(4,2), bhmcdate.Mid(6,2) );

memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, " PAGE : %3d DATE : %s%c%c", page, strDate, CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );

memset(sbuf, 0x00, sizeof(sbuf));
// 123456789/123456789/123456789/123456789/123456789/1234567890123456789012345678901234567890
sprintf(sbuf, "========================================================%c%c", CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );

memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, " SEQ BODY NO BUMP ECOL T/L ENG BHMCTIME%c%c%c%c", CR, LF, CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );

memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, "========================================================%c%c", CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );
}

strTime.Format("%s:%s", bhmcdate.Mid(8,2), bhmcdate.Mid(10,2) );

memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, " %-4.4s %-10.10s %-4.4s %-3.3s %-4.4s %-4.4s %-5.5s%c%c%c%c",
seq, body, alc, ecol, tl, eng, strTime, CR, LF, CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );

memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, "--------------------------------------------------------%c%c", CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );

prt_ret = PrintStatus();
if ( prt_ret == 1 ) {
for ( i=0; i<3; i++ ) {// CKSEQTMP FLAG03 UPDATE
if ( CkseqtmpUpdate() == TRUE ) break;
else Sleep(1000);
}

cnt = cnt + 1;
if ( cnt > 7) { // 8 LINE PRINT
memset(sbuf, 0x00, sizeof(sbuf));
sprintf(sbuf, "%c%c%c", FF, CR, LF);
ret = send( sid, sbuf, strlen(sbuf), 0 );
page = page + 1;
cnt = 0;
}
}
else {
printf("Printer Status Check !!!/n");
}

Sleep(3000);
}

closesocket(sid);
WSACleanup();
Sqldb.Close();
return nRetCode;
}

//
// DB OPEN
BOOL DbOpen()
{
if(Sqldb.Open("DSN=bjmodule;UID=sa;PWD=bjmodule")) {
Sqldb.m_bOpen = TRUE;
return TRUE;
}
else {
return FALSE;
}
}

/
// CKSEQTMP FLAG03='0' SELECT

你可能感兴趣的:(c,数据库,语言,initialization,database,mfc)