Create Access data source name dynamically

This sample in this article was created using VC 5.0. The necessary .h and .lib files are present in the zip.

To create a DSN at run time you could use the SQLConfigDataSource API. Internally this information is stored in the registry. The syntax is attached below

SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0","DSN=TestDB\0DBQ=D:\\Database\\Friends.mdb\0DEFAULTDIR=D:\\DATABASE\0\0");

The problem is that if you want to accept the values from the user or set these values at run time by passing CString or char* will not work. This is because, when sprintf encounters a /0 it assumes it is the end of the string and ignores the rest of the data.

As a workaround you have to use the below mentioned code.

The following code places : where a /0 is expected and there is a loop which replaces a ":" with "/0". You will not be in a position to use sprintf because, when it encounters a /0 it assumes it is the end of the string and ignores the rest of the data.

char* szDesc;

int mlen;

szDesc=new char[256];

sprintf(szDesc,"DSN=%s: DESCRIPTION=TOC support source: DBQ=%s: FIL=MicrosoftAccess: DEFAULTDIR=D:\\Database:: ","TestDB","D:\\Friends.mdb");

mlen = strlen(szDesc);

for (int i=0; i<mlen; i++)

{

if (szDesc[i] == ':')

szDesc[i] = '\0';

}

if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))

MessageBox(hwnd,"Failed","INFORMATION",MB_OK);

else

MessageBox(hwnd,"Sucess","INFORMATION",MB_OK);

Download ZIP - 48KB



Comments

  • cXiwwH NI HZ VwK Kyfe XH

    Posted by TJXckuLtHs on 02/17/2013 01:27pm

    order tramadol online tramadol withdrawal for dogs - tramadol withdrawal long

    Reply
  • Goal

    Posted by snareenactina on 11/02/2012 04:44pm

    France is one of Europe’s biggest players in the Information and Communications Technologies (ICT) segment because of its large pool of skilled workforce and a strong proclivity for innovation. alcippe This does not mean that equilibrating forces do not exist. It means only that we must consider open ended processes in markets. This is not a new proposition. Adam Smith, the founder of economics as a distinct discipline, thought in evolutionary and process orientated terms. vivienne spiked warrant noble stolem

    Reply
  • Same solution using a pointer

    Posted by Legacy on 12/15/2002 12:00am

    Originally posted by: Juan Subotich

    A slightly optimized solution using a pointer, and changing
    
    the ':' character by '#' because of posible conflicts if
    it is used in de path name of the database :

    char buffer [256];

    sprintf(buffer,"DSN=%s#DBQ=%s#DEFAULTDIR=%s#",
    strDSNName,
    strDSNPath,
    strDSNDir);

    char * p=buffer;


    while(*p != '\0')
    {
    if(*p == '#')
    *p='\0';
    p++;
    }

    Reply
  • abou access connectivity

    Posted by Legacy on 09/07/2002 12:00am

    Originally posted by: sachin

    hi
    this is sachin
    and i want to connect database as access
    but it gives error so tell me the proper way to connect it

    Reply
  • Java-Access combination

    Posted by Legacy on 06/12/2002 12:00am

    Originally posted by: sanjay

    hi
    this is sanjay
    presently i am doing a project on java which has backend as instant db but i want to replace it with access and things should be done dynamically.if any help plz mail me asap
    thank u.

    Reply
  • The Same Problem Of UID,PWD

    Posted by Legacy on 05/26/2002 12:00am

    Originally posted by: Noura

    The Same Problem Of UID,PWD

    I need My client to connect to sql server using ODBC using the function SQLConfigDataSource every thing is OK but I can't specify user and password for this connection ...
    When not writting UID,PWD attributes ,An authentication Dialog box displayed for asking the user to verify the user and password....and it's not desired to display such a message...
    I need Help....PLZ

    Reply
  • Adding UID and PWD fails

    Posted by Legacy on 04/11/2002 12:00am

    Originally posted by: Rajesh

    When I create a DSN for SQL Server everything works fine, except a UID and PWD is specified. If they are not specified registration succeeds using Win NT Login as the Login for SQL Server. Has anybody tried this?

    Reply
  • Creating MDB with password Dynamically

    Posted by Legacy on 02/15/2002 12:00am

    Originally posted by: nagendra

    i 'am able to create a mdb dynamically using CREATE_DB in SQLConfigDataSource
    can any one help me create mdb with password dynamically


    Reply
  • How can i see a report from access in VC++ using ADO?

    Posted by Legacy on 01/20/2002 12:00am

    Originally posted by: Nuno Rapaz

    How can i see a report from access in VC++ using ADO?
    I don't no very well how reports do, but what i want is to execute querys from VC++ in reports from access.
    I can do it in tables but i don't no if it's possible to do it in reports.

    thanks

    Reply
  • Ms Access 2000 Data Types

    Posted by Legacy on 10/29/2001 12:00am

    Originally posted by: Suji

    Hi
    This is Suji
    I am Using MS Access 2000 as Backend for Java Applications.
    If you have any idea on Ms Access DataTypes those will be used in Java Programming pl tell me.
    Or if u found any web page contains brief Description about Ms Access 2000 Datatypes pl suggest me
    Thanksinadvance
    Yours
    Suji

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds