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

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds