Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Adding a data source question

Adding a data source question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comhelptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hiseldl
    wrote on last edited by
    #1

    I've searched the code project site, but I have been unable to find any articles that show how to add a system dsn through code. Could someone point me to a snippet of C++ that registers a DSN in the ODBC Data Sources? I appreciate any help you can give me. :) David Hisel -- http://www.hisel.com/

    B P 2 Replies Last reply
    0
    • H hiseldl

      I've searched the code project site, but I have been unable to find any articles that show how to add a system dsn through code. Could someone point me to a snippet of C++ that registers a DSN in the ODBC Data Sources? I appreciate any help you can give me. :) David Hisel -- http://www.hisel.com/

      B Offline
      B Offline
      Bill Wilson
      wrote on last edited by
      #2

      I don't have any code, but I can tell you how I've done it. Its really pretty easy. You need to add some entries to the system registry. These entries define a DSN named APDB. [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\APDB] "Driver"="C:\\WINNT\\System32\\SQLSRV32.dll" "Server"="odyw2ksql02" "Database"="Odyssey_APDB" "LastUser"="obuser" [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources] "APDB"="SQL Server"

      H 1 Reply Last reply
      0
      • B Bill Wilson

        I don't have any code, but I can tell you how I've done it. Its really pretty easy. You need to add some entries to the system registry. These entries define a DSN named APDB. [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\APDB] "Driver"="C:\\WINNT\\System32\\SQLSRV32.dll" "Server"="odyw2ksql02" "Database"="Odyssey_APDB" "LastUser"="obuser" [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources] "APDB"="SQL Server"

        H Offline
        H Offline
        hiseldl
        wrote on last edited by
        #3

        Thanks! This is very helpful, I appreciate it. :) David Hisel -- http://www.hisel.com/

        1 Reply Last reply
        0
        • H hiseldl

          I've searched the code project site, but I have been unable to find any articles that show how to add a system dsn through code. Could someone point me to a snippet of C++ that registers a DSN in the ODBC Data Sources? I appreciate any help you can give me. :) David Hisel -- http://www.hisel.com/

          P Offline
          P Offline
          Pavel Klocek
          wrote on last edited by
          #4

          Here is some code to do this:

          BOOL CreateSource(LPCSTR lpszDriver, LPCSTR lpszAttributes, BOOL bSystem /*= TRUE*/)
          {
          //nahradit '$' nulou ('\0') v lpszAttributes
          char attr[500];
          char driver[250];

          if(lpszAttributes)
          {
          	int mlen = strlen(lpszAttributes);
          	strcpy(attr,lpszAttributes);
          	char\* ch = attr;
          	for (int i=0; i<mlen; i++,ch++) {if (\*ch == '$') \*ch = '\\0';}
          }
          else
          {
          	attr\[0\] = 0;
          }
          
          //Pridani druhe nuly za retezec s driverem
          if(lpszDriver)
          {
          	int mlen = strlen(lpszDriver);
          	strcpy(driver,lpszDriver);
          	strcat(driver,"\\0");
          }
          else
          {
          	driver\[0\] = 0;
          }
          
          WORD request;
          if(bSystem) request = ODBC\_ADD\_SYS\_DSN;
          else request = ODBC\_ADD\_DSN;
          
          //zavolani ODBC API
          return SQLConfigDataSource(NULL,request,driver,attr);
          

          }

          BOOL CreateAccessSource(LPCSTR lpszName, LPCSTR lpszPath, LPCSTR lpszDescription /*= NULL*/, BOOL bSystem /*= TRUE*/)
          {
          char* szDesc;
          szDesc=new char[500];

          if(lpszDescription)
          	sprintf(szDesc,"DSN=%s$ DESCRIPTION=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszDescription,lpszPath);
          else
          	sprintf(szDesc,"DSN=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszPath);
          
          return CreateSource("Microsoft Access Driver (\*.mdb)",(LPCSTR)szDesc,bSystem);
          

          }

          Pavel Sonork 100.15206

          H 1 Reply Last reply
          0
          • P Pavel Klocek

            Here is some code to do this:

            BOOL CreateSource(LPCSTR lpszDriver, LPCSTR lpszAttributes, BOOL bSystem /*= TRUE*/)
            {
            //nahradit '$' nulou ('\0') v lpszAttributes
            char attr[500];
            char driver[250];

            if(lpszAttributes)
            {
            	int mlen = strlen(lpszAttributes);
            	strcpy(attr,lpszAttributes);
            	char\* ch = attr;
            	for (int i=0; i<mlen; i++,ch++) {if (\*ch == '$') \*ch = '\\0';}
            }
            else
            {
            	attr\[0\] = 0;
            }
            
            //Pridani druhe nuly za retezec s driverem
            if(lpszDriver)
            {
            	int mlen = strlen(lpszDriver);
            	strcpy(driver,lpszDriver);
            	strcat(driver,"\\0");
            }
            else
            {
            	driver\[0\] = 0;
            }
            
            WORD request;
            if(bSystem) request = ODBC\_ADD\_SYS\_DSN;
            else request = ODBC\_ADD\_DSN;
            
            //zavolani ODBC API
            return SQLConfigDataSource(NULL,request,driver,attr);
            

            }

            BOOL CreateAccessSource(LPCSTR lpszName, LPCSTR lpszPath, LPCSTR lpszDescription /*= NULL*/, BOOL bSystem /*= TRUE*/)
            {
            char* szDesc;
            szDesc=new char[500];

            if(lpszDescription)
            	sprintf(szDesc,"DSN=%s$ DESCRIPTION=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszDescription,lpszPath);
            else
            	sprintf(szDesc,"DSN=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszPath);
            
            return CreateSource("Microsoft Access Driver (\*.mdb)",(LPCSTR)szDesc,bSystem);
            

            }

            Pavel Sonork 100.15206

            H Offline
            H Offline
            hiseldl
            wrote on last edited by
            #5

            This is great! Thank you. Note: your code was mis-interpreted as HTML inside the first if block, look at the for loop to see what happened. Could you send this via email to david at hisel dot com? Thanks again, David Hisel -- http://www.hisel.com/

            P 1 Reply Last reply
            0
            • H hiseldl

              This is great! Thank you. Note: your code was mis-interpreted as HTML inside the first if block, look at the for loop to see what happened. Could you send this via email to david at hisel dot com? Thanks again, David Hisel -- http://www.hisel.com/

              P Offline
              P Offline
              Pavel Klocek
              wrote on last edited by
              #6

              I fixed it. Pavel Sonork 100.15206

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups