⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqltestn.c

📁 本代码展示了使用DB_LIB库对SQL SERVER的连接和各种操作的实例。
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
Copyright (c) 2000, Microsoft Corporation
All Rights Reserved.
***********************************************************************/
/***********************************************************************

	PROGRAM: SqlTestn.c
	
	PURPOSE: SqlTest sample Windows applications

	FUNCTIONS:

	WinMain() - calls initialization function, processes message loop
	SqlTestInit() - initializes window data and registers window
	SqlTestWndProc() - processes messages
	AboutSQL() - processes messages for "About" dialog box
	SelectSQL() - processes input of author name
	ConnectSQL() - processes input of server name and connects to server

	COMMENTS:

	WIN32 can have several copies of your application running at the
	same time.  The variable hInst keeps track of which instance this
	application is so that processing will be to the correct window.

	You only need to initialize the application once.  After it is
	initialized, all other copies of the application will use the same
	window class, and do not need to be separately initialized.

***********************************************************************/
// This sample uses mixed mode security, other than Windows NT Authentication, 
// to establish connections. To use Windows NT Authentication, please use 
// DBSETLSECURE to set the secure connection flag.
// Make the necessary changes to the hard-coded values, such as server 
// name, user name and password.

#include "windows.h"      /* required for all NT Windows applications*/
#include "stdio.h"
#define DBNTWIN32         /* needed to define environment 	    */
#include "sqlfront.h"     /* standard dblib include file	    */
#include "sqldb.h"        /* standard dblib include file	    */
#include "sqltestn.h"     /* specific to this program		    */

PDBPROCESS dbproc = (PDBPROCESS)NULL;
	              /* dbprocess pointer for dblib connection*/
HANDLE hInst;    /* current instance			    */
HWND ghWnd;      /* global window handle for handlers    */
HWND errhWnd;    /* global window handle for current error*/

/****************************************************************************

	FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)

	PURPOSE: calls initialization function, processes message loop

	COMMENTS:

	This will initialize the window class if it is the first time this
	application is run.  It then creates the window, and processes the
	message loop until a PostQuitMessage is received.  It exits the
	application by returning the value passed by the PostQuitMessage.

****************************************************************************/

int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
HINSTANCE hInstance;			     /* current instance	     */
HINSTANCE hPrevInstance;			     /* previous instance	     */
LPSTR lpCmdLine;			     /* command line		     */
int nCmdShow;				     /* show-window type (open/icon) */
{
	HWND hWnd;				     /* window handle		     */
	MSG msg;				     /* message			     */


	if (!hPrevInstance)			/* Has application been initialized? */
		if (!SqlTestInit(hInstance))
			return (0);		/* Exits if unable to initialize     */

	hInst = hInstance;			/* Saves the current instance	     */

	hWnd = CreateWindow("SQL Test",               /* window class	     */
		"SQL Server Sample Windows NT Application", /* window name	     */
		WS_OVERLAPPEDWINDOW|WS_VISIBLE,            /* window style	     */
		CW_USEDEFAULT,                             /* x position		     */
		CW_USEDEFAULT,                             /* y position		     */
		CW_USEDEFAULT,                             /* width		     */
		CW_USEDEFAULT,                             /* height		     */
		NULL,                                      /* parent handle	     */
		NULL,                                      /* menu or child ID	     */
		hInstance,                                 /* instance		     */
		NULL);                                     /* additional info	     */

	if (!hWnd)					  /* Was the window created? */
		return (0);

	ghWnd = hWnd;				  /* set global handle	     */
	errhWnd = hWnd;

	ShowWindow(hWnd, SW_SHOW); /* Shows the window        */
	UpdateWindow(hWnd);        /* Sends WM_PAINT message  */

	while (GetMessage(&msg, /* message structure */
				NULL,		      /* handle of window receiving the message */
				0,             /* lowest message to examine */
				0))            /* highest message to examine	*/
	{
		TranslateMessage(&msg);	   /* Translates virtual key codes	     */
		DispatchMessage(&msg);	   /* Dispatches message to window	     */
	}
	return (msg.wParam);	   /* Returns the value from PostQuitMessage */
}


/****************************************************************************

	FUNCTION: SqlTestInit(HANDLE)

	PURPOSE: Initializes window data and registers window class

	COMMENTS:

	Sets up a structure to register the window class.  Structure includes
	such information as what function will process messages, what cursor
	and icon to use, etc.

****************************************************************************/

BOOL SqlTestInit(hInstance)
HANDLE hInstance;			       /* current instance	     */
{
	HANDLE hMemory;			       /* handle to allocated memory */
	PWNDCLASS pWndClass;		       /* structure pointer	     */
	BOOL bSuccess;			       /* RegisterClass() result     */

	hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
	pWndClass = (PWNDCLASS)hMemory;

	pWndClass->style = 0; /*CS_HREDRAW | CS_VREDRAW; */
	pWndClass->lpfnWndProc = (WNDPROC)SqlTestWndProc;
	pWndClass->hInstance = hInstance;
	pWndClass->hIcon = LoadIcon(hInstance, "SQLITEST");
	pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
	pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
	pWndClass->lpszMenuName = (LPSTR)"SQLTest";
	pWndClass->lpszClassName = (LPSTR)"SQL Test";

	bSuccess = RegisterClass(pWndClass);

	LocalFree(hMemory);				    /* Returns it to NT */
	return (bSuccess);		 /* Returns result of registering the window */
}

/****************************************************************************

	FUNCTION: SqlTestWndProc(HWND, unsigned, WORD, LONG)

	PURPOSE:  Processes messages

	MESSAGES:

	WM_SYSCOMMAND - system menu (About dialog box)
	WM_CREATE     - create window
	WM_DESTROY    - destroy window
	WM_COMMAND    - application menus (Connect and Select dialog boxes

	COMMENTS:

	To process the ID_ABOUTSQL message. Call Dialog
	box which will create the box according to the information in your
	SqlTest.rc file and turn control over to the About() function.	When
	it returns, free the intance address.
	This same action will take place for the two menu items Connect and
	Select.

****************************************************************************/

LONG APIENTRY SqlTestWndProc(hWnd, message, wParam, lParam)
HWND hWnd;				  /* window handle		     */
UINT message;	        		  /* type of message		     */
DWORD wParam;				  /* additional information	     */
LONG lParam;				  /* additional information	     */
{
					  /* functions 			     */
	HMENU hMenu;			  /* handle to the System menu	     */

	switch (message)
	{
		case WM_SYSCOMMAND:		/* message: command from system menu */
			if (wParam == ID_ABOUTSQL)
			{
				DialogBox(hInst,		 /* current instance	     */
					(LPTSTR)"ABOUTSQL",		 /* resource to use	     */
					hWnd,			 /* parent handle	     */
					(DLGPROC)AboutSQL);		 /* About() instance address */

				break;
			}
			else			    /* Lets NT process it	     */
				return (DefWindowProc(hWnd, message, wParam, lParam));

		case WM_CREATE:			    /* message: window being created */

			/* Get the handle of the System menu */
			hMenu = GetSystemMenu(hWnd, FALSE);

			/* Add a separator to the menu */
			AppendMenu(hMenu,			      /* menu handle	     */
				MF_SEPARATOR,				      /* menu item to change */
				0,				      /* new menu item	     */
				0);				      /* menu identifier     */

			/* Add new menu item to the System menu */
      	AppendMenu(hMenu,			      /* menu handle	     */
				MF_STRING,				      /* menu item to change */
				ID_ABOUTSQL,			      /* menu identifier     */
				"A&bout SQL Test...");		      /* new menu item	     */

			dbinit();                                 /* initialize dblib    */
	
			/* Now make the message and error    */
			/* Install the handler into dblib */	
			dberrhandle((DBERRHANDLE_PROC)dbwinErrorHandler);
			dbmsghandle((DBMSGHANDLE_PROC)dbwinMessageHandler);
			break;
	
		case WM_COMMAND :			/* menu selections generate */
				
			/* the WM_COMMAND message   */	
			switch(wParam)			/* menu in WORD parameter   */
			{
				case IDM_CONNECT :		/* connect to server	    */
					DialogBox(hInst,		/* current instance	     */
						"CONNECT",	 	/* resource to use	     */
						hWnd,			/* parent handle	     */
						(DLGPROC)ConnectSQL); 	/* ConnectSQL() instance address */

					break;
	
				case IDM_SELECT :		/* select an author	    */

					DialogBox(hInst,		 /* current instance	     */
						"SELECT",		 /* resource to use	     */
						hWnd,			 /* parent handle	     */
						(DLGPROC)SelectSQL);		 /* About() instance address */

					break;
			}
			break;
	
		case WM_DBRESULTS :			/* a select has been issued */
			SqlTestProcessResults(hWnd);	/* process results	    */
			break;

		case WM_DESTROY:		  /* message: window being destroyed */
			dbexit();			  /* free any active dbprocesses     */
			PostQuitMessage(0);
			break;

		default:			  /* Passes it on if unproccessed    */
			return (DefWindowProc(hWnd, message, wParam, lParam));
	}
	
	return (0L);
}


/****************************************************************************

	FUNCTION: AboutSQL(HWND, unsigned, WORD, LONG)

	PURPOSE:  Processes messages for "AboutSQL" 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 AboutSQL(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
DWORD wParam;
LONG lParam;
{
	switch (message)
	{
		case WM_INITDIALOG:		   /* message: initialize dialog box */
			return (TRUE);

		case WM_COMMAND:		      /* message: received a command */
			if (wParam == IDOK)
			{	      /* "OK" box selected?	     */
				EndDialog(hDlg, 0);	      /* Exits the dialog box	     */
				return (TRUE);
			}
			break;
	}
	return (FALSE);			      /* Didn't process a message    */
}
/****************************************************************************

	FUNCTION: SelectSQL(HWND, unsigned, WORD, LONG)

	PURPOSE:  Processes messages for "SelectSQL" 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.
	
	Let user input into edit control the name of an author (the select
	IS case sensitive).  When user presses OK, format the select statement
	then send it to the server and execute it via dbsqlexec(). If the
	dbsqlexec() SUCCEED's post a WM_DBRESULTS message so the results
	may be retrieved and processed.

	Wait for user to click on "Ok" button, then close the dialog box.

****************************************************************************/

LONG APIENTRY SelectSQL(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
DWORD wParam;
LONG lParam;
{
	char szSelectAuthor[41];		  /* string for authors name	    */
	char szServerMess[45];		  /* string for server response	    */
	char szAName[40];			  /* format string for author	    */
	switch (message)
	{
		case WM_INITDIALOG:		   /* message: initialize dialog box */
			SendDlgItemMessage(hDlg,       /* limit input to 40 characters   */
				AUTHORNAME,EM_LIMITTEXT,40,0L);
			return (TRUE);

		case WM_COMMAND:		      /* message: received a command */
			errhWnd = hDlg;
			switch(wParam)
			{
				case IDOK :		      /* "OK" box selected?	     */
					*szSelectAuthor = '\0';   /* Null author		     */
					GetDlgItemText(hDlg,AUTHORNAME, (LPSTR)szSelectAuthor, MAX_ANAME);
					if(dbproc == (PDBPROCESS)NULL) /* if not a valid process*/
					{
						/* No server to query		*/
						MessageBox(hDlg,
							"No SQL Server Connected to Query",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -