home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / mdibind / dsndlg.cpp next >
C/C++ Source or Header  |  1998-03-26  |  3KB  |  107 lines

  1. // DSNDlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "MDIBind.h"
  15. #include "DSNDlg.h"
  16. #include <sqlext.h>
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDataSrcDlg dialog
  26.  
  27.  
  28. CDataSrcDlg::CDataSrcDlg(CString &DSN,CString &Sql,CString &User,CString &Pw,
  29.                          CWnd* pParent /*=NULL*/)
  30.     : CDialog(CDataSrcDlg::IDD, pParent)
  31. {
  32.     //{{AFX_DATA_INIT(CDataSrcDlg)
  33.     m_DSN = DSN;
  34.     m_User = User;
  35.     m_Sql = Sql;
  36.     m_Pw = Pw;
  37.     //}}AFX_DATA_INIT
  38. }
  39.  
  40.  
  41. void CDataSrcDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43.     CDialog::DoDataExchange(pDX);
  44.     //{{AFX_DATA_MAP(CDataSrcDlg)
  45.     DDX_Text(pDX, IDC_DATASRC, m_DSN);
  46.     DDX_Text(pDX, IDC_USER, m_User);
  47.     DDX_Text(pDX, IDC_SQL, m_Sql);
  48.     DDX_Text(pDX, IDC_PASWORD, m_Pw);
  49.     //}}AFX_DATA_MAP
  50. }
  51.  
  52.  
  53. BEGIN_MESSAGE_MAP(CDataSrcDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CDataSrcDlg)
  55.     //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CDataSrcDlg message handlers
  60.  
  61. BOOL CDataSrcDlg::OnInitDialog()
  62. {
  63.     CDialog::OnInitDialog();
  64.  
  65.     HENV henv;
  66.     RETCODE nRetCode;
  67.     SWORD cbName = -1;
  68.     SWORD cbDescription = -1;
  69. #ifdef _UNICODE
  70.     TCHAR szName[SQL_MAX_DSN_LENGTH + 256]; //safety
  71.     TCHAR szDesc[512+1]; //enough
  72.     TCHAR *comboName=szName;
  73. #else
  74.     UCHAR szName[SQL_MAX_DSN_LENGTH + 256]; //safety
  75.     UCHAR szDesc[512+1]; //enough
  76.     char *comboName=(char *)szName;
  77. #endif
  78.  
  79.     CComboBox* pCombo = (CComboBox*) GetDlgItem(IDC_DATASRC);
  80.  
  81.     // Only returns SQL_SUCCESS or SQL_ERROR, Can assume errors mem alloc errors
  82.     if(SQLAllocEnv(&henv) != SQL_SUCCESS)
  83.     {
  84.         AfxMessageBox(_T("ODBC error"));
  85.         return TRUE;
  86.     }
  87.     do
  88.     {
  89.         nRetCode = ::SQLDataSources(henv, SQL_FETCH_NEXT,
  90.             szName, SQL_MAX_DSN_LENGTH, &cbName,
  91.             szDesc, 512, &cbDescription);
  92.         if(nRetCode != SQL_SUCCESS && nRetCode != SQL_SUCCESS_WITH_INFO)
  93.             break;
  94.         szName[cbName] = '\0';
  95.         pCombo->AddString(comboName);
  96.     }while(1);
  97.  
  98.     if(nRetCode == SQL_ERROR)
  99.         AfxMessageBox(_T("Data Source Retrieve error"));
  100.     nRetCode = SQLFreeEnv(henv);
  101.     if(nRetCode == SQL_ERROR)
  102.         AfxMessageBox(_T("ODBC error"));
  103.  
  104.     return TRUE;  // return TRUE unless you set the focus to a control
  105.                   // EXCEPTION: OCX Property Pages should return FALSE
  106. }
  107.