Create Access data source name dynamically | CodeGuru

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)”,”DSN=TestDBDBQ=D:\Database\Friends.mdbDEFAULTDIR=D:\DATABASE”); The problem is that […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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)","DSN=TestDBDBQ=D:\Database\Friends.mdbDEFAULTDIR=D:\DATABASE");

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] = '';
}
if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)",(LPCSTR)szDesc))
  MessageBox(hwnd,"Failed","INFORMATION",MB_OK);
else
  MessageBox(hwnd,"Sucess","INFORMATION",MB_OK);

Download ZIP – 48KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.