#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }    return FALSE;
}BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_OK:
DBTest(hwnd);
        break;
        default:
break;
    }
}void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("???????"),MB_ICONERROR|MB_OK);
}void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={SQL Server Native Client 10.0};SERVER={NECK-PC};UID=mysa;PWD=123456;DATABASE='重命名之后';CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
SQLSMALLINT   cbConnStrOut = 0;// Make connection without data source. Ask that driver 
// prompt if insufficient information. Driver returns
// SQL_ERROR and application prompts user
// for missing information. Window handle not needed for
// SQL_DRIVER_NOPROMPT.
result = SQLDriverConnect(hdbc,      // Connection handle
                  NULL,         // Window handle
                  ConnStrIn,      // Input connect string
                  SQL_NTS,         // Null-terminated string
                  ConnStrOut,      // Address of output buffer
                  MAXBUFLEN,      // Size of output buffer
                  &cbConnStrOut,   // Address of output length
                  SQL_DRIVER_PROMPT); if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String, 
//please alculate the string length for me   
result = SQLPrepare(hstmt,(SQLCHAR*)"Insert Into 球员资料(姓名,国籍,身价) values('贝克汉姆','英国',800000)",SQL_NTS);CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("插入成功"),TEXT("提示"),MB_OK);
}