Je trouve pas le bon code

vah bi Messages postés 3 Date d'inscription vendredi 25 février 2005 Statut Membre Dernière intervention 29 septembre 2008 - 29 sept. 2008 à 03:27
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 29 sept. 2008 à 12:03
Bonjour à tous , voilà je débute en c et comme editeur j'ai le visual c++.
Je  fais actuellement une application avec l'api de visual c++  mais en langage c.
J'utilise comme source de données ODBC pour ma connexion à ma bd.
mais je sais pas comment écrire les requetes pour atteindre mes donnée (select, insert into...)
Quelle aide pouvez vous me conseiller  pour un débutant

voici mon code:

// RTI.cpp : Defines the entry point for the application.
//
"

#include "stdafx.h"

#include "resource.h"

#include "sql.h"

#include "sqlext.h"
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];

SQLHENV     henv;
SQLHDBC     hdbc;
SQLHSTMT    hstmt;
SQLRETURN retcode;
                                // The title bar text

// Foward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    Emission(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    Present(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    Pconnect(HWND, UINT, WPARAM, LPARAM);
RECT rt;
voidconexion(HWND );

//declaration de la variable de traitement d'environnement et de connection
/* allouer l'environnement de connexion */

void connexion(HWND hDlg)
{

SQLINTEGER      cbInt=0, cbChar = SQL_NTS;

      /*Allocate environment handle */
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

   /* Set the ODBC version environment attribute */
   retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

  
      /* Allocate connection handle */
      retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

         /* Connect to data source */
         retcode = SQLConnect(hdbc, (SQLCHAR*) "connRTI", SQL_NTS,
                  (SQLCHAR*) "sa", SQL_NTS,
                  (SQLCHAR*) "sqlserver", SQL_NTS);        
 
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_RTI, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_RTI);

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_IRTI);//IDI_RTI);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = (LPCSTR)IDC_RTI;
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_IRTI);//IDI_SMALL);

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

    switch (message)
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_ABOUT:
                   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
                   break;
                case IDM_EXIT:
                   DestroyWindow(hWnd);
                   break;
                case IDM_EMIS:
                    DialogBox(hInst, (LPCTSTR)IDD_EM, hWnd, (DLGPROC)Emission );
                    break;

                case IDM_PRE:
                        //DialogBox(hInst, (LPCTSTR)IDD_PR, hWnd, (DLGPROC)Present);
                          DialogBox(hInst, (LPCTSTR)IDD_PCNXOK, hWnd, (DLGPROC)Pconnect);
                        break;
                default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            //RECT rt;
            GetClientRect(hWnd, &rt);
            DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
                return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }
    return FALSE;
}

// Mesage handler for Emission box.
LRESULT CALLBACK Emission(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

    retcode= SQLPrepare(hdbc,"select * from tbl_emission",0);
    switch (message)
    {
        case WM_INITDIALOG:
                retcode= SQLExecute(hdbc);
                DialogBox(hInst, (LPCTSTR)IDC_LBE, hdbc, (DLGPROC)Emission);
               
                return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDEXIT)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            if (LOWORD(wParam) == IDC_VALIDER)

            {
                DialogBox(hInst, (LPCTSTR)IDD_PR, hDlg, (DLGPROC)Present);
                //EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }
    return FALSE;
}

// Mesage handler for Présentateur box.
LRESULT CALLBACK Present(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
                return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)

            {   
            //    DialogBox(hInst, (LPCTSTR)IDD_PCNXOK, hDlg, (DLGPROC)Pconnect);
            //    EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            if (LOWORD(wParam) ==IDC_RET)

            {   
               
            EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }
    return FALSE;
}

 
LRESULT CALLBACK Pconnect(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
       
    switch (message)
    {
        case WM_INITDIALOG:
                return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDQUIT)

            {
               
                EndDialog(hDlg, LOWORD(wParam));
            //    EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }else {
            if (LOWORD(wParam) == IDACPT)

            {
               
                EndDialog(hDlg, LOWORD(wParam));
            //    EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            }
            break;
    }
    return FALSE;

}

1 réponse

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
29 sept. 2008 à 12:03
Quelques remarques:
Dans une dialogbox, la plupart des messages doivent retourner 0 hors WM_INITDIALOG (1).
L'API est de Windows, pas de VC++.

pour ODBC, voir:
http://www.cppfrance.com/code.aspx?ID=27746
et ceci:
http://www.google.com/custom?domains=cppfrance.com&q=ODBC&sa=Rechercher&sitesearch=cppfrance.com

ciao...
BruNews, MVP VC++
0
Rejoignez-nous