2 Replies Latest reply on Apr 18, 2018 1:18 PM by Tom Smith

    How to get SQL server name from PDM API?

    Tom Smith

      I have a PDM addin that uses some SQL queries directly to the database.  I am trying to find a way to get the server name itself from the API, so I don't have to hardcode it in to the connection string.  I didn't see anything useful in IEdmVault(n).  Is there some easy way?  As a last resort i could probably read C:\{vault}\desktop.ini and pick thru it.

        • Re: How to get SQL server name from PDM API?
          Lee CS Young

          Use the registry.

           

          public static string GetServerName(string vaultName)
                  {
                      if (string.IsNullOrEmpty(vaultName))
                          return string.Empty;
                      Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey($@"SOFTWARE\SolidWorks\Applications\PDMWorks Enterprise\Databases\{vaultName}", false);
                      if (key != null)
                      {
                          return key.GetValue("DBServer").ToString();
                      }
                      return null;
                  }
                  public static string GetDatabaseName(string vaultName)
                  {
                      if (string.IsNullOrEmpty(vaultName))
                          return string.Empty;
                      Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey($@"SOFTWARE\SolidWorks\Applications\PDMWorks Enterprise\Databases\{vaultName}", false);
                      if (key != null)
                      {
                          return key.GetValue("DbName").ToString();
                      }
                      return null;
                  }