How to Create a Connection String for an ODBC System, User or File DSN

The other type of data that you can create by using the Data Access Library is a data source name (DSN). Like a universal data link (UDL) file, a DSN contains the connection information necessary to access a remote database. However, where a .udl file is generally used for an OLE DB database connection, most systems use DSN files for ODBC connections. Further, a DSN does not necessarily have to be a file: a DSN can be a file, a registry-based system DSN, or a registry-based user DSN.

  1. Call the constructor for DB2OdbcFileConnectionString, DB2OdbcSysConnectionString, or DB2OdbcUserConnectionString.

    Calling the constructor creates a connection string with default settings. These default settings can be set only through the Data Access Tool user interface.

    If you use a file path for a file that currently exists, the system loads the connection string information in that file instead.

  2. Fill in the relevant connection string properties by calling the properties of the object created.

    You can also fill in the connection string with GetString, which returns the connection string as a text string. After you finish modifying the relevant values, you can return the connection string to the object with a call to SetString.

  3. Save the connection string information back into storage with a call to Save.

The following code example demonstrates how to create a DSN file and associated connection string.

static DB2OdbcFileConnectionString CreateUDLFile2(string FileName, string NameOfUser, string PassWord, ref System.Exception myException)
{
   try
   {
      DB2OdbcFileConnectionString myConnection = new DB2OdbcFileConnectionString(FileName, false);
      myConnection.UserName = NameOfUser;
      myConnection.Password = PassWord;
      myConnection.Save();
      System.Exception MyEx= new System.Exception(@"Successful Creation", null);
      myException = MyEx;
      return myConnection;

   }
   catch (Exception ex)
   {
      myException = ex;
      return null;

   }
}

Community Additions

ADD
Show: