The ODBC API
SQLConfigDataSource function can be used to create new SQL Server DSNs
programmatically. For example, the following code creates a new System DSN
named MyNewDSN (or modifies if it already exists) connecting to the pubs
database using the TCP/IP protocol:
returnCode = SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN, (LPSTR) "SQL Server",
(LPSTR) "DSN=MyNewDSN\0"
"Network=DBMSSOCN\0"
"Server=MySqlServer\0"
"Database=pubs\0"
"Description=New Data Source\0");
Creating a new DSN enters or modifies the default network library value
for SQL Servers stored in the registry under the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo
The DSQUERY string under this registry key contains
the default network library for any SQL Server.
If the network
library name that is shown in the
SQLConfigDataSource call above is coded as "Network=dbmssocn\0" instead of
"Network=DBMSSOCN\0", the
Client Configuration dialog box will show the network library as "Other".