+ Reply to Thread
Results 1 to 4 of 4

Thread: Create .odc file

  1. #1
    Mele Koneya
    Join Date
    Apr 2010
    Posts
    159
    Points
    2
    Answers Provided
    6


    0

    Default 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.

    Thanks,

    Mele



    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();
    }

  2. #2
    Kirk Kuykendall

    Join Date
    Oct 2009
    Posts
    295
    Points
    3
    Answers Provided
    0


    0

    Default 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.

    Code:
    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
    Kirk Kuykendall
    AmberGIS Programming Services
    Now answering questions at http://gis.stackexchange.com

  3. #3
    Mele Koneya
    Join Date
    Apr 2010
    Posts
    159
    Points
    2
    Answers Provided
    6


    0

    Thumbs up Re: Create .odc file

    Thanks! This gave me exactly what I was looking for. I too wonder what ArcLife will be without VBA

  4. #4
    Mele Koneya
    Join Date
    Apr 2010
    Posts
    159
    Points
    2
    Answers Provided
    6


    0

    Default 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))
    {
    File.Delete(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
    SdeWorkspaceFactoryClass();
    return workspaceFactory.Create(SDEdir, SDEfile, propertySet, 0);
    }

    Any help is appreciated.

    Thanks,

    Mele
    Attached Files

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts