📄 sqltestn.c
字号:
"SQL Test",MB_ICONHAND | MB_OK);
}
else
if(*szSelectAuthor != '\0') /* if a name exists */
{
DBLOCKLIB(); /* lock down the library */
/* format the select statement */
dbcmd(dbproc,
(LPSTR)"select au_id, au_lname,"
"au_fname, phone, address, city, state, zip");
dbcmd(dbproc, (LPSTR)" from authors");
dbcmd(dbproc, (LPSTR)" where au_lname = ");
sprintf(szAName,"'%s'",szSelectAuthor);
dbcmd(dbproc,(LPSTR)szAName);
if (dbsqlexec(dbproc) == FAIL)
{
sprintf(szServerMess, /* error, not in db */
"%s not found in database pubs",
szSelectAuthor);
MessageBox(hDlg,
(LPSTR)szServerMess,(LPSTR)"SQL Test",
MB_ICONHAND | MB_OK);
}
else /* query SUCCEEDed so */
{ /* post message to process results */
PostMessage(GetParent(hDlg),WM_DBRESULTS,0,0L);
}
DBUNLOCKLIB(); /* unlock library */
}
EndDialog(hDlg, 0); /* Exits the dialog box */
return (TRUE);
break;
case IDCANCEL :
EndDialog(hDlg, 0); /* cancelled select */
return(TRUE);
break;
}
break;
}
return (FALSE); /* Didn't process a message */
}
/****************************************************************************
FUNCTION: ConnectSQL(HWND, unsigned, WORD, LONG)
PURPOSE: Processes messages for "Connect" dialog box
MESSAGES:
WM_INITDIALOG - initialize dialog box
WM_COMMAND - Input received
COMMENTS:
No initialization is needed for this particular dialog box, but TRUE
must be returned to NT.
Wait for user to click on "Ok" button, then close the dialog box.
****************************************************************************/
LONG APIENTRY ConnectSQL(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
DWORD wParam;
LONG lParam;
{
char szSQLServer[31];
static PLOGINREC LoginRec;
*szSQLServer = '\0';
switch (message)
{
case WM_INITDIALOG: /* message: initialize dialog box*/
SendDlgItemMessage(hDlg, /* limit input to 30 characters */
SQL_SERVER,EM_LIMITTEXT,30,0L);
return (TRUE);
case WM_COMMAND: /* message: received a command*/
errhWnd = hDlg;
switch(wParam)
{
case IDOK : /* "OK" box selected? */
GetDlgItemText(hDlg,SQL_SERVER,
(LPSTR)szSQLServer,
MAX_SERVERNAME); /* get Server name */
if(*szSQLServer != '\0') /* was something input */
{
DBLOCKLIB(); /* lock down library */
if (dbproc != (PDBPROCESS)NULL) /* if an active */
/* process close it */
dbclose(dbproc);
if ((LoginRec = dblogin()) != (PLOGINREC)NULL) /* get loginrec */
{
DBSETLUSER(LoginRec,(char far *)"sa"); /* set user */
DBSETLVERSION(LoginRec,DBVER60);
// To use secure, or trusted, connection, uncomment the following line.
// DBSETLSECURE (login);
/* now open the connection to server */
if((dbproc = dbopen(LoginRec,(LPSTR)szSQLServer))
== (PDBPROCESS)NULL)
{
/* if NULL couldn't connect */
dbfreelogin(LoginRec);
}
else /* got connect so use the pubs database */
{
dbuse(dbproc,(LPSTR)"pubs");
dbfreelogin(LoginRec);
}
}
else /* memory allocation problem */
MessageBox(hDlg, "Could not allocate Login Record","System Error", MB_ICONHAND | MB_OK);
DBUNLOCKLIB(); /* done unlock library */
}
EndDialog(hDlg, 0); /* Exits the dialog box */
return (TRUE);
break;
case IDCANCEL :
EndDialog(hDlg, 0);
return(TRUE);
break;
}
break;
}
return (FALSE); /* Didn't process a message */
}
/****************************************************************************
FUNCTION: CheckForScroll(HWND, int, int, int)
PURPOSE: Check if next output line will be out of client area
PARAMETERS:
hWnd - Handle to the window.
CurrentPosition - Current y coordinate for the line of
text just written to the client area.
Spacing - The height of the line (including the space
separating lines) of the text just written.
Length - The length of the line just written in device units.
RETURN: Returns the Y coordinate for the next line of text.
COMMENTS:
Will determine if the next line of text will be out of the client
area. If so will scroll the window for the next line. Also validates
the current line of text so that a WM_PAINT will not clear it.
****************************************************************************/
int CheckForScroll(hWnd,CurrentPosition,Spacing, Length)
HWND hWnd;
int CurrentPosition;
int Spacing;
int Length;
{
RECT rect; /* RECT structure for validation */
rect.top = CurrentPosition; /* top of last line of text */
rect.bottom = CurrentPosition+Spacing+1; /* bottom of last line */
rect.left = 1; /* left most column of line */
rect.right = Length+1; /* right most column of line */
ValidateRect(hWnd,(LPRECT)&rect); /* validate line so that it is */
/* not blanked on next paint */
GetClientRect(hWnd,(LPRECT)&rect); /* get rect for current client */
if(CurrentPosition + (Spacing*2) > rect.bottom) /* will line fit */
{
/* if not scroll window and */
/* update client window */
ScrollWindow(hWnd,0,-(Spacing+1),NULL,NULL);
UpdateWindow(hWnd);
return(CurrentPosition);
}
return(CurrentPosition+Spacing);
}
/****************************************************************************
FUNCTION: SQLTestProcessResults(HWND)
PURPOSE: If a valid dbprocess is present process all results from pending
select statement, output each field to client area. Whenever
a new line is written to client area it is checked to see if
the client area needs to be scrolled.
PARAMETERS: hWnd - Handle to the window.
RETURN: Returns the Y coordinate for the next line of text.
COMMENTS:
This function will bind the fields in the select statement
to local variables, format an output string then
write that string to the client area via TextOut.
It is called by the main message processing loop
SQLTestWndProc via the message WM_DBRESULTS.
****************************************************************************/
BOOL SqlTestProcessResults(hWnd)
HWND hWnd;
{
HDC hDC; /* display context */
TEXTMETRIC tm; /* text metric structure */
char szId[12]; /* Author ID for binding */
char szLastName[41]; /* Author last name for binding */
char szFirstName[21]; /* Author first name for binding */
char szPhone[13]; /* Author phone for binding */
char szAddress[41]; /* Author address for binding */
char szCity[21]; /* Author city for binding */
char szState[3]; /* Author state for binding */
char szZip[6]; /* Author zipcode for binding */
char szOutputString[81]; /* general output string */
RETCODE result_code; /* results code from dbresults */
int Y; /* Y coordinate for text output */
int Spacing; /* Spacing between lines */
errhWnd = hWnd;
hDC = GetDC(hWnd); /* get display context */
GetTextMetrics(hDC, (LPTEXTMETRIC)&tm); /* get font info */
Spacing = tm.tmExternalLeading + tm.tmHeight; /* set up spacing */
Y = 1; /* start at line 1 */
if(dbproc == (PDBPROCESS)NULL) /* if process null, no results */
{
ReleaseDC(hWnd,hDC); /* free resources and return */
return(TRUE);
}
SendMessage(hWnd,WM_ERASEBKGND,(DWORD)hDC,0L); /* always erase background */
UpdateWindow(hWnd); /* force painting of window */
DBLOCKLIB(); /* lock down library */
/* get all results from the query*/
while(((result_code = dbresults(dbproc)) != NO_MORE_RESULTS) && result_code != FAIL)
{
if(result_code == SUCCEED) /* if results ready */
{
/* Bind all data of interest */
dbbind(dbproc,1,NTBSTRINGBIND, 12L, (LPSTR)szId);
dbbind(dbproc,2,NTBSTRINGBIND, 41L, (LPSTR)szLastName);
dbbind(dbproc,3,NTBSTRINGBIND, 21L, (LPSTR)szFirstName);
dbbind(dbproc,4,NTBSTRINGBIND, 13L, (LPSTR)szPhone);
dbbind(dbproc,5,NTBSTRINGBIND, 41L, (LPSTR)szAddress);
dbbind(dbproc,6,NTBSTRINGBIND, 21L, (LPSTR)szCity);
dbbind(dbproc,7,NTBSTRINGBIND, 3L, (LPSTR)szState);
dbbind(dbproc,8,NTBSTRINGBIND, 6L, (LPSTR)szZip);
while(dbnextrow(dbproc) != NO_MORE_ROWS) /* get all rows */
{
/* here we format each field and write it to client */
/* area checking to see if the client area needs to */
/* be scrolled after each line is written */
sprintf(szOutputString,"Author ID: %s",szId);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
sprintf(szOutputString,"Last Name: %s",szLastName);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
sprintf(szOutputString,"Address: %s",szAddress);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
sprintf(szOutputString,"City: %s",szCity);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
sprintf(szOutputString,"State: %s",szState);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
sprintf(szOutputString,"ZipCode: %s",szZip);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
sprintf(szOutputString,"Telephone: %s",szPhone);
TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
Y = CheckForScroll(hWnd,Y,Spacing,0); /* add extra line */
/* after each results */
}
}
}
DBUNLOCKLIB(); /* unlock library */
ReleaseDC(hWnd,hDC); /* free resource */
return(TRUE);
}
/****************************************************************************
FUNCTION: dbwinMessageHandler(PDBPROCESS, DBINT, DBSMALLINT, DBSMALLINT,
LPSTR)
PURPOSE: When the Data Server returns a message to dblib this function
will be called to process that message. This function is
installed into dblib via MakeProcInstance. It must be declared
as a FAR cdecl function, not as a FAR PASCAL function, unlike
other call back routines, as dblib conducts all of it's calls
in the cdecl fashion. You must return 0 to dblib.
RETURN: Return 0
COMMENTS:
****************************************************************************/
int API dbwinMessageHandler(dbproc, msgno, msgstate, severity, msgtext)
DBPROCESS *dbproc;
DBINT msgno;
INT msgstate;
INT severity;
CHAR *msgtext;
{
MessageBox(errhWnd,msgtext,(LPSTR)"SQL DataServer Message",MB_OK);
return(0);
}
/****************************************************************************
FUNCTION: dbwinErrorHandler(PDBPROCESS, int, int, int, LPSTR, LPSTR)
PURPOSE: When dblib returns an error message to the application this
function will be called to process that error. This function is
installed into dblib via MakeProcInstance. It must be declared
as a FAR cdecl function, not as a FAR PASCAL function, unlike
other call back routines, as dblib conducts all of it's calls
in the cdecl fashion. You must return either INT_CANCEL,
INT_CONTINUE, or INT_EXIT to dblib.
RETURN: Return continuation code.
COMMENTS:
****************************************************************************/
int API dbwinErrorHandler(dbproc, severity, errno, oserr, dberrstr, oserrstr)
PDBPROCESS dbproc;
int severity;
int errno;
int oserr;
LPSTR dberrstr;
LPSTR oserrstr;
{
MessageBox(errhWnd,dberrstr,(LPSTR)"DB-LIBRARY error",MB_ICONHAND | MB_OK);
if (oserr != DBNOERR) /* os error */
MessageBox(errhWnd,oserrstr,(LPSTR)"Operating-System error",MB_ICONHAND | MB_OK);
return(INT_CANCEL); /* cancel command */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -