DSN-less configuration

In a DSN-less configuration, the odbc.ini file is not consulted for server connection properties. To connect to a servername, your application may refer to a servername entry in freetds.conf, or explicitly specify the servername's hostname (bypassing freetds.conf).

Example 4.1. Sample files for a DSN-less configuration

The odbcinst.ini is quite brief:

	;
	; odbcinst.ini
	;
	[FreeTDS]
	Driver = /usr/local/freetds/lib/libtdsodbc.so
	

The freetds.conf might look something like:

	;
	; freetds.conf
	;
	[JDBC]
	host = jdbc.sybase.com
	port = 4444
	tds version = 5.0
	


Example 4.2. Connecting with a DSN-less configuration

	/*
	 * application call
	 */
	const char servername[] = "JDBC"; [9]
	sprintf(tmp, "DRIVER=FreeTDS[10];SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;",
		servername, username, password, dbname);
	res = SQLDriverConnect(Connection, NULL, (SQLCHAR *) tmp, SQL_NTS,
				(SQLCHAR *) tmp, sizeof(tmp), &len, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(res)) {
		printf("Unable to open data source (ret=%d)\n", res);
		exit(1);
	}
	


You can even establish a connection without reference to either odbc.ini or freetds.conf.

Example 4.3. Connecting with a DSN-less configuration that does not use freetds.conf

	/*
	 * application call
	 */
	const char servername[] = "jdbc.sybase.com"; [11]
	sprintf(tmp, "DRIVER=FreeTDS[10];SERVER=%s;UID=%s;PWD=%s;DATABASE=%s;TDS_Version=5.0;Port=4444;",
		servername, username, password, dbname);
	res = SQLDriverConnect(Connection, NULL, (SQLCHAR *) tmp, SQL_NTS,
				(SQLCHAR *) tmp, sizeof(tmp), &len, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(res)) {
		printf("Unable to open data source (ret=%d)\n", res);
		exit(1);
	}
	




[9] refers to the [JDBC] entry in freetds.conf.

[10] refers to the [FreeTDS] entry in odbcinst.ini.

[11] refers to the real server name.