Win32 API 和 ODBC 访问数据库一

准备用Win32 API和ODBC来访问数据库;

代码如下;

/*------------------------------------------------------------
   win32, ODBC, by bobo, 2018-09-09
  ------------------------------------------------------------*/

#include <windows.h>
#include "sql.h"
#include "sqlext.h"
#include "sqltypes.h"
#include "sqlucode.h"
#include "odbcss.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
#define DSNAME "testDS1"
#define DSUSERNAME "sa"
#define DSUSERPWD "123"

#pragma comment(lib, "odbc32.lib")

SQLRETURN ret;
SQLHENV henv;
SQLHDBC hdbc;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello ODBC"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          300,              // initial x size
                          100,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
	 DWORD err;
     
     switch (message)
     {
     case WM_CREATE:
	  		  
		  ret= SQLAllocEnv(&henv);	
		  ret = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
		  //ret = SQLConnect(hdbc, (unsigned char *)"testDS1", SQL_NTS, (unsigned char *)"sa", SQL_NTS,(unsigned char *)"123", SQL_NTS);
		  ret = SQLConnect(hdbc, (SQLCHAR*)"testDS41", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS,(SQLCHAR*)"123", SQL_NTS);
		  //err=GetLastError();
		  if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) //成功连接到数据库
		  {
			  MessageBox (NULL, TEXT ("SUCCESS"), TEXT ("HelloODBC"), 0);
		  }
		  else
		  {
			  MessageBox (NULL, TEXT ("FAIL"), TEXT ("HelloODBC"), 0);
		  }
		  return 0 ;
          
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          
          GetClientRect (hwnd, &rect) ;
          
          DrawText (hdc, TEXT ("Hello, ODBC!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          
          EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

在调试的时候发现SQLConnect总是返回1,不知道是否连接成功;
使用ODBC的预定义值来判断是否连接成功;
原来,
SQL_SUCCESS的enum值为0, SQL_SUCCESS_WITH_INFO的enum值为1;

进入调试,查看一下;
henv和hdbc两个句柄已经分配成功;ret为1;

这两种写法一样;
ret = SQLConnect(hdbc, (unsigned char *)"testDS1", SQL_NTS, (unsigned char *)"sa", SQL_NTS,(unsigned char *)"123", SQL_NTS);
ret = SQLConnect(hdbc, (SQLCHAR*)"testDS41", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS,(SQLCHAR*)"123", SQL_NTS);
    
SQL_NTS参数的位置,本来是字符串的长度,此处可以用预定义的SQL_NTS代替;
NTS => "Null-Terminated String"

如果参数对就连接成功,改个不对的参数,例如"testDS101",则会失败;

如果构建时报错
cannot open Debug/XXXX.exe for writing
关闭开发环境,重来;或者看任务管理器里,正在做的程序是否还在运行,结束之即可;

另外需要先在系统中建立ODBC数据源;

先做这么多吧,这活老难了;

截图如下;

   

猜你喜欢

转载自blog.csdn.net/bcbobo21cn/article/details/82535634
今日推荐