3 Replies Latest reply on Jun 19, 2010 4:29 PM by mkoneya

    Create .odc file

      I would like to create an .odc file to connect to SQL Server 2005 within a C# project.    I am using the SQL connection to create an XYTheme from a table and would like the .odc connection to be created and discarded after use each time the program runs.

      Here is the code that I have so far.    Each time I debug it,   the  'Add OLD DB Connection' form is launched.     I think the problem may be with the Properties that I am using.    I have not found any examples of how to do this, so any help would be appreciated.



      private static string CreateODBCconnectionFile(string server,  string user, string password)
                  IPropertySet propSet = new PropertySetClass();

                  propSet.SetProperty("Provider", "SQL Native Client");
                  propSet.SetProperty("Data Source", server);
                  propSet.SetProperty("User name", user);
                  propSet.SetProperty("Password", password);
                  IWorkspaceFactory2 pWSFact = (IWorkspaceFactory2)new OLEDBWorkspaceFactory();

                  return pWSFact.Create("d:\\temp\\", "test.odbc", propSet, 0).ToString();
        • Re: Create .odc file
          I never can remember the properties ... nor where to find them in the documentation.
          So sometimes what I do is set up a connection in ArcCatalog, select it in the treeview, then run vba code that lists the properties, then write .NET code that sets those properties.  It's doing things like this that makes me worry about life without VBA.

          Option Explicit
          Sub ListConnProps()
              Dim pGxApp As IGxApplication
              Set pGxApp = Application
              If Not TypeOf pGxApp.SelectedObject Is IGxDatabase Then
                  Debug.Print "select a geodb first"
                  Exit Sub
              End If
              Dim pGXdb As IGxDatabase2
              Set pGXdb = pGxApp.SelectedObject
              Dim names As Variant, values As Variant
              Debug.Print pGXdb.WorkspaceName.WorkspaceFactoryProgID
              pGXdb.WorkspaceName.ConnectionProperties.GetAllProperties names, values
              Dim l As Long
              For l = 0 To UBound(names)
                  Debug.Print names(l), values(l)
              Next l
          End Sub
          • Re: Create .odc file
            Thanks!    This gave me exactly what I was looking for.       I too wonder what ArcLife will be without VBA
            • Re: Create .odc file
              After stepping away from this code for a while to do other things.    I am back to it and not having much success.

              I was able to extract the connection properties from my existing ODBC connection, but each time I try to create a connection file through ArcObjects I am giving the Data Link Properties Form from ArcGIS Desktop.

                private static IWorkspaceName CreateODBCconnectionFile()
                          IPropertySet propSet = new PropertySetClass();
                          propSet.SetProperty("Provider", "SQLNCLI");
                          propSet.SetProperty("Password", "pwd");
                          propSet.SetProperty("Persist Security Info", "True");        
                          propSet.SetProperty("User ID", "UID");
                          propSet.SetProperty("Initial Catalog", "ComDevPrmt");
                          propSet.SetProperty("Data Source", "server");

                          IWorkspaceFactory2 pWSFact = (IWorkspaceFactory2)new OLEDBWorkspaceFactoryClass();
                                      propSet.GetAllProperties(out names, out values);


                          return (IWorkspaceName)pWSFact.Create(@"d:\workspace\", "tmpfile.odc", propSet,0);

              I have been able to create an .sde file using similar code as shown below.

              public static IWorkspaceName CreateConnectionFile(string server, string instance, string user, string password, string database, string version, string SDEdir, string SDEfile)

                          string SDEFullName = SDEdir + SDEfile;
                          if (File.Exists(SDEFullName))
                          IPropertySet propertySet = new PropertySetClass();
                          propertySet.SetProperty("SERVER", server);
                          propertySet.SetProperty("INSTANCE", instance);
                          propertySet.SetProperty("DATABASE", database);
                          propertySet.SetProperty("USER", user);
                          propertySet.SetProperty("PASSWORD", password);
                          propertySet.SetProperty("VERSION", version);
                          IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new
                          return workspaceFactory.Create(SDEdir, SDEfile, propertySet, 0);

              Any help is appreciated.